javascript - Socket.IO basic example not working -
i'm 100% new socket.io , have installed it. trying follow examples, , can server side running can't seem client side connected.
the following server.js:
var http = require('http'), io = require('socket.io'), server = http.createserver(function(req, res){ res.writehead(200, {'content-type': 'text/html'}); res.end('<h1>hello world</h1>'); }); server.listen(8090); var socket = io.listen(server); socket.on('connection', function(client){ console.log('client connected.'); client.on('message', function(){ console.log('message.'); client.send('lorem ipsum dolor sit amet'); }); client.on('disconnect', function(){ console.log('disconnected.'); }); });
this index.html
<!doctype html> <html lang="en"> <head> <title>socket example</title> <base href="/" /> <meta charset="utf-8" /> <script src="http://localhost:8090/socket.io/socket.io.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script> </head><body> <script type="text/javascript"> $(document).ready(function(){ var socket = new io.socket('localhost', { 'port': 8090 }); socket.connect(); console.log("connecting."); socket.on('connect', function () { console.log("connected."); }); socket.on('message', function (msg) { console.log("message: " + msg + "."); }); socket.on('disconnect', function () { console.log("disconnected."); }); }); </script> </body></html>
when node server.js indicates socket.io started.
when load index.html line comes indicating "debug - served static /socket.io.js" nothing else, no console messages or other lines.
any guidance provide appreciated. said i'm 100% green if break down as possible i'd appreciate it.
thanks
in same origin policy, localhost:8090
, localhost
not same origin (different port), localhost/index.html
cannot connect socket.io on localhost:8090.
one option make index.html on localhost:8090
(see code below). put "index.html" same directory of server script, start server , type localhost:8090
in browser.
var fs = require('fs'); server = http.createserver(function(req, res){ fs.readfile(__dirname + '/index.html', 'utf-8', function(err, data) { // read index.html in local file system if (err) { res.writehead(404, {'content-type': 'text/html'}); res.end('<h1>page not found</h1>'); } else { res.writehead(200, {'content-type': 'text/html'}); res.end(data); } }); });
Comments
Post a Comment