Node.js, express and socket.io : socket.on doesn't receive message socket.emit from the server to the client -
this function doesn't work in app. i can't receive message (socket.emit) from server client (socket.on). don't have problem in inverse (client server).
i use cloud9 , chat example them works fine. here code server :
var http = require('http'); var path = require('path'); var async = require('async'); var socketio = require('socket.io'); var express = require('express'); var router = express(); var server = http.createserver(router); var io = socketio.listen(server); router.use(express.static(path.resolve(__dirname, 'client2'))); io.on('connection', function (socket) { socket.emit('hello', 'i changed'); // !!! }); server.listen(process.env.port || 3000, process.env.ip || "0.0.0.0", function(){ var addr = server.address(); console.log("chat server listening at", addr.address + ":" + addr.port); });
and code html page :
<html> <head> <title>test</title> <script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect(); socket.on('hello', function (msg) { // !!! document.innerhtml = msg; }); </script> </head> <body> <p>normal page</p> </body> </html>
and there no change... edit : console.log either. message seems sent socket :
debugger listening on port 15454 info - socket.io started chat server listening @ 0.0.0.0:8080 debug - served static content /socket.io.js debug - client authorized info - handshake authorized 13xpsyguxzmpmeyfsrfo debug - setting request /socket.io/1/websocket/13xpsyguxzmpmeyfsrfo debug - set heartbeat interval client 13xpsyguxzmpmeyfsrfo debug - client authorized debug - websocket writing 1:: debug - websocket writing 5:::{"name":"hello","args":["i changed"]}
can me please ?
you need change
var socket = io.connect();
by
var socket = io.connect('http://0.0.0.0:8080');
Comments
Post a Comment