5

I have a problem.

Client side code

 <html> <body onload="fun()"> <script src="C:\cygwin\usr\local\lib\node\.npm\socket.io\0.6.16\package\support\socket.io-client\socket.io.js"></script> <script> function fun() { alert("hello") var socket = new io.Socket('localhost',{'port':8090}); socket.on('connect', function(){ socket.send('hi!'); }) socket.on('connect', function(){ console.log('connected to server');socket.send('hi there, this is a test message'); }) socket.on('message', function(data){ alert(data); }) socket.on('disconnect', function(){}) } </script> </body> </html> 

server side code:

var http = require('http'), io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io') server = http.createServer(function(req, res){ // your normal server code res.writeHead(200, {'Content-Type': 'text/html'}); res.end('<h1>Hello world</h1>'); }); server.listen(8090); // socket.io var socket = io.listen(server); socket.on('connection', function(client){ // new client is here! client.on('message', function(){ console.log('message arrive'); }) client.on('disconnect', function(){ console.log('connection closed');}) }); 

found this example from socket.io. When I run the server it gives me Socket io is ready. Accepting connection when I run the browser it is not showing anything and also on the firefox firebug console please help me to solve this problem.

1 Answer 1

6

You never call socket.connect() on the client side so the socket never tryes to connect to the server. Check the following code :

Client side ->

<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> <script src="socket.io.js"></script> <script> function fun() { var socket = new io.Socket('localhost',{'port':8090}); socket.connect(); socket.on('connect', function(){ console.log('connected'); socket.send('hi!'); }); socket.on('message', function(data){ console.log('message recived: ' + data); }); socket.on('disconnect', function(){ console.log('disconected'); }); } $(document).ready(function() { fun(); }); </script> </head> <body> </body> </html> 

Server side ->

var http = require('http'), io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io') server = http.createServer(function(req, res){ // your normal server code res.writeHead(200, {'Content-Type': 'text/html'}); res.end('<h1>Hello world</h1>'); }); server.listen(8090); // socket.io var socket = io.listen(server); socket.on('connection', function(client){ // new client is here! client.on('message', function(){ console.log('message arrive'); client.send('some message'); }); client.on('disconnect', function(){ console.log('connection closed'); }); }); 
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for reply, but i face the same problem ...that code is working for you??? i run the code like this:.....on cygwin runserver node server.js and then run the html file is this the right way for running
One problem would be the correct path to socket.io on the client side, please check with firebug if the script loaded and adjust/change/move_the_file till you get it loaded in the browser <script src="socket.io.js"></script> . Yes it's working here like a charm , i even whent further and sent mesages back end forth with setInterval on both client and server ....
thanks poelinca its work....thanks for help...can you tell mee any tutorial on node js and socket .......
you're welcomed, no i don't know any tutorial about sockets in node but google comes with some good examples

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.