I’m using NodeJS v0.4.8 and the latest Version of socket.io from
npm install socket.io
on Ubuntu:
Linux mars 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux
The following code unfortunately doesn't produce any output, wheter on client, nor on server side.
Does anybody have a clue?
SERVER-SIDE
var http = require('http'),
io = require('socket.io'),
fs = require('fs'),
sys = require('sys');
respcont = fs.readFileSync('testclient.js');
server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(respcont);
});
server.listen(8082);
var socket = io.listen(server);
socket.on('connection', function(client){
sys.puts("New client is here!");
client.send("hello world");
client.on('message', function(msg) { sys.puts("client has sent:"+msg); }) ;
client.on('disconnect', function() { sys.puts("Client has disconnected"); }) ;
});
CLIENT-SIDE
<html>
<body>
<script type="text/javascript" src="http://localhost:8082/socket.io/socket.io.js"></script>
<script>
var socket = new io.Socket(null,{port:8082,rememberTransport:true,timeout:1500});
socket.connect();
socket.on('connect', function() {
console.log('connected to server');
socket.send('Hi Server...');
});
socket.on('message', function() {
console.log('received a message!');
});
socket.on('disconnect', function() {
console.log('disconnected from server');
});
</script>
</body>
</html>
The output from NodeJS (NOT the sys.puts("...") calls) is:
info - socket.io started debug - served static /socket.io.js debug - client authorized info - handshake authorized info - handshaken b61a5c2751c1c8c8493db4b79d19e779
I took your example and dropped it in an a node app using express. Your HTML code was placed in a static HTML file under public. Your example worked fine. The code is shown below. I wanted to make sure both the socket.io script file and the HTML file were being served up properly.
I also(like Derrish) like to use express framework to simplify my work(AWESOME :)). You can download and extract this sample from http://dl.dropbox.com/u/314941/socketio.zip. I believe you don't even have to install these modules because I have bundled them locally(just run) thanks to npm :).
How to install:
The code:
app.js:
public/index.html:
Listing of my modules:
Installed modules(NOT necessary):
Browser will display:
socket.io
on start, but probably you can't see this because it will be replaced withconnected
.connected
when the user connects to socket.io.Waited two seconds!
Express 3.0 + Socket.io working example
server ( app.js )
client ( index.html )
you can fork the code using the link below https://github.com/sreekumar-kr/Expree3.0---Socket.IO