Skip to content

Instantly share code, notes, and snippets.

@hopewise
Created March 28, 2013 09:27
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 hopewise/5261902 to your computer and use it in GitHub Desktop.
Save hopewise/5261902 to your computer and use it in GitHub Desktop.
I am serving a static page, which will connect to socket.io at my node js app, I don't know why socket connection event takes about 10 seconds! as a result the server response takes about 10 seconds to reach the client, I added a link at html page to test the time, emit and emit call back, it takes about one second .. also, its too long .. pleas…
// server static file
var fs = require('fs'),
http = require('http');
var app= http.createServer(function (req, res) {
console.log(__dirname);
fs.readFile(__dirname + req.url, function (err,data) {
if (err) {
res.writeHead(404);
res.end(JSON.stringify(err));
return;
}
res.writeHead(200);
res.end(data);
});
});
var io = require('socket.io').listen(app);
app.listen(8080);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('client_click', function (data) {
this.emit('client_click_callback', { data_from_server: data.client_data });
});
socket.on('my other event', function (data) {
console.log(data);
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Paul Engine</title>
<meta name="description" content="" />
<script src="/socket.io/socket.io.js"></script>
<script>
console.log("trying to connect");
var socket = io.connect(); // 'http://hopewise-helloworld.jit.su'
var counter= 0;
socket.on('news', function (data) {
console.log(data);
document.getElementById('message').innerHTML=data.hello;
socket.emit('my other event', { my: 'data from hopewise ...' });
});
socket.on('client_click_callback', function (data) {
console.log(data);
document.getElementById('message').innerHTML=data.data_from_server;
});
</script>
</head>
<body>
<div id="haxe:trace"></div>
<textarea id="message">
connecting ...
</textarea>
<a href="#" onclick="emit_event();return false;">Call Socket</a>
<script>
function emit_event()
{
socket.emit('client_click', { client_data: 'data from hopewise ...' + (counter++).toString() });
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment