Created
July 23, 2012 21:02
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> | |
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Client side: