javascript - socket.emit not passing messages -
i have following js code
`var express = require('express'); var app = express(); var http = require('http').server(app); var path = require("path"); var io = require('socket.io')(http); app.get('*', function (req, res){ res.sendfile(path.join(__dirname, '/public')); }); app.use('/home',express.static(path.join(__dirname,'/public'))); //app.use('/static', express.static(__dirname + 'index.html')); io.on('connection', function (socket) { socket.on('message', function (data) { socket.emit('news', { hello: 'world' }); }); socket.on('another-message', function (data) { socket.emit('not-news', { hello: 'world' }); }); }); http.listen(3000, function(){ console.log('listening on *:3000'); });`
i have html code
<html> <h1>working</h1> <script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost:3000', { path :'/' +home}); socket.on('connect',function(){ socket.emit('message', 'hello server'); }); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script> <body> <p>display message</p> </body> </html>`
i go page localhost:3000/home , html page. in console couldn't see messages. wrong? please correct me.
why didn't errors in console? got error. i'll paste screenshot of this:
click on see you've got errors.
i'll save time.
change line in index.html
var socket = io.connect('http://localhost:3000', { path :'/' +home});
to
var socket = io.connect('http://localhost:3000');
after you'll this:
Comments
Post a Comment