Reputation: 21
I am new to node.js/socket.io/websocket and i am trying to create a web chat application. Currently i am stuck on 1 problem for days : no matter how hard i try i can't make the browser/server communication via websocket.
My server code :
var express = require("express");
var app = express();
var port = 3700;
var io = require('socket.io').listen(app.listen(port));
console.log("Listening on port " + port);
io.sockets.on('connection', function (socket) {
console.log("connect");
socket.emit('globalmessage', { message: 'welcome to the chat !' });
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
My Client code :
var messages = [];
var socket = io.connect('http://localhost:3700');
var field = document.getElementById("field");
var sendButton = document.getElementById("send");
var content = document.getElementById("content");
var name = 'test';
var role = 1;
socket.on('message', function (data) {
if(data.message) {
messages.push(data);
var html = '';
for(var i=0; i<messages.length; i++) {
html += messages[i].message + '<br />';
}
content.innerHTML = html;
} else {
console.log("There is a problem:", data);
}
});
socket.on('globalmessage', function (data) {
content.innerHTML += data.message;
});
sendButton.onclick = function() {
var text = field.value;
socket.emit('send', { message: text, username: name, role: userrole});
};
When i start node js server, and go to client via browser(both chrome and firefox) i got those data in cmd windows :
info - socket.io started
Listening on port 3700
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized 7492ni4I5TCkRNvfG8WU
debug - setting request GET /socket.io/1/websocket/7492ni4I5TC
debug - set heartbeat interval for client 7492ni4I5TCkRNvfG8WU
debug - client authorized for
debug - websocket writing 1::
connect
debug - websocket writing 5:::{"name":"globalmessage","args":[
come to the chat !"}]}
debug - emitting heartbeat for client 7492ni4I5TCkRNvfG8WU
debug - websocket writing 2::
debug - set heartbeat timeout for client 7492ni4I5TCkRNvfG8WU
As you can see, there are websocket writing 5:::{"name":"globalmessage","args":[ Welcome to the chat !"}]} so that mean the server can read when client is connected, and did emit the message back to client, but at client there are nothing happen, i can't read the message, the event is not fired at all. When i try to send message from client to server also nothing happen.
On firebug console i saw there are 1 success request to address
localhost:3700/socket.io/1/?t=1399976614584
with response 7492ni4I5TCkRNvfG8WU:60:60:websocket
If i change the transport from websocket to xhr-polling everything work perfectly, the client can receive server message, can send back message to server, so i think the code is fine (actually i copied the code from http://code.tutsplus.com/tutorials/real-time-chat-with-nodejs-socketio-and-expressjs--net-31708 ).
So can you please help me on what's wrong with websocket setup at my local ? I have no idea what went wrong since everything work fine with ajax polling and i really need it to work with websocket instead. All my test was done in my local : windows 7 home premium, xampp 3.1.0 (websocket didn't work even if i turned apache off)
Thank you all !
Upvotes: 0
Views: 1357
Reputation: 658
I have used socket io with express on a project before, but i have never had this issue. I can share the code that i have with you which this
var io = require('socket.io');
var io_connection = io.listen(server, {
origins: '*:*',
log: true,
'heartbeat timeout': 6000,
'close timeout': 6000,
'log level': 3,
'transports': [
'websocket',
'flashsocket',
'htmlfile',
'xhr-polling',
'jsonp-polling'
]
});
io_connection
.on('connection', function(socket) {
user(socket); // bind user socket connection
poll(socket); // bind user's socket to poll related sockets
});
return io;
Hope it helps
Upvotes: 0