Skip to content

Instantly share code, notes, and snippets.

@lancehunt
Created July 23, 2012 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancehunt/3166214 to your computer and use it in GitHub Desktop.
Save lancehunt/3166214 to your computer and use it in GitHub Desktop.
Basic socket.io example that fails in safari. Run server with node then navigate to https://localhost:9000
<html>
<head>
<div>Check your debug window for websocket failures in Safari</div>
<div>Should give you this error: WebSocket network error: The operation couldn’t be completed. (OSStatus error -2146885613.) </div>
<script src="/socket.io/socket.io.js"></script>
<script>
var baseUrl = window.location.protocol + "//" + window.location.host, // assumes client is hosted on same url/port as socket.io websockets
socket = io.connect(baseUrl);
socket.on('news', function (data) {
document.write('<p>' + JSON.stringify(data) + ' from ' + baseUrl + '</p>');
setTimeout(function () {
socket.emit('my other event', { my: 'data', timestamp: new Date() });
}, 1000);
});
</script>
</head>
<body></body>
</html>
var fs = require('fs')
, privateKey = fs.readFileSync('key.pem').toString()
, certificate = fs.readFileSync('cert.pem').toString()
, options = {key: privateKey, cert: certificate}
, app = require('https').createServer(options, handler)
// , app = require('http').createServer(handler) // Swap the above line and this one to change between ssl and non-ssl
, io = require('socket.io').listen(app);
// TO generate pem files execute these commands and follow the prompts:
//openssl genrsa -out key.pem 1024
//openssl req -new -key key.pem -out certrequest.csr
//openssl x509 -req -in certrequest.csr -signkey key.pem -out cert.pem
app.listen(9000);
function handler(req, res) {
fs.readFile(__dirname + '/client.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading client.html');
}
res.writeHead(200);
res.end(data);
});
}
io.configure(function () {
io.enable('browser client minification');
io.enable('browser client etag');
io.enable('browser client gzip');
io.set('log level', 1);
io.set('transports', [
'websocket'
]);
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'Connected', timestamp: new Date() });
socket.on('my other event', function (data) {
console.log(data);
setTimeout(function () {
socket.emit('news', { hello: 'world', timestamp: new Date() });
}, 1000);
});
});
@yanivge
Copy link

yanivge commented Jan 29, 2013

We tried to use this example. it took us 2 days to finaly make it work. unfortenetly there is not enought debug information to work with

few things need to change:
Server side:

  1. good valid certificate (self signed can work here as well)
  2. do not force a transport, allow all. io.set('transports', ['websocket', 'flashsocket', 'htmlpage', 'xhr-polling', 'jsonp-polling', ]);

Client side:

  1. remove the "document.write" and its content. use alert ("got something"). its bad syntax for some browsers.
  2. use "socket = io.connect(baseUrl,{secure:true});"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment