I'm not sure exactly how to understand the usage of message in socket.io. I'm trying to send message from the client to server then the server reply to the client (basic nah?).
This is the server code:
var fs = require('fs');
var hskey = fs.readFileSync('ssl.key');
var hscert = fs.readFileSync('ssl.crt');
var options = {
key: hskey,
cert: hscert
};
var app = require('https').createServer(options);
var io = require('/usr/local/lib/node_modules/socket.io').listen(app);
app.listen(8181);
io.sockets.on('connection', function (socket) {
socket.emit('serverMessage', 'Hello world!');
io.sockets.emit('serverMessage', 'A user connected');
io.sockets.on('clientMessage', function (socket) {
socket.emit('serverMessage', 'Gotamessage bitch!');
console.log('I received a message by ', from, ' saying ', msg);
});
socket.on('disconnect', function () {
io.sockets.emit('serverMessage', 'A user disconnected');
});
});
This is the client code:
<script>
var socket;
var firstconnect = true;
function connect() {
if(firstconnect) {
socket = io.connect(':8181');
socket.on('serverMessage', function(data){ message(data); });
socket.on('connect', function(){ status_update("Connected to Server"); });
socket.on('disconnect', function(){ status_update("Disconnected from Server"); });
socket.on('reconnect', function(){ status_update("Reconnected to Server"); });
socket.on('reconnecting', function( nextRetry ){ status_update("Reconnecting in "
+ nextRetry + " seconds"); });
socket.on('reconnect_failed', function(){ message("Reconnect Failed"); });
firstconnect = false;
}
else {
socket.socket.reconnect();
}
}
function disconnect() {
socket.disconnect();
}
function message(data) {
document.getElementById('message').innerHTML += "<br>" + "Server says: " + data;
}
function status_update(txt){
document.getElementById('status').innerHTML = txt;
}
function esc(msg){
return msg.replace(/</g, '<').replace(/>/g, '>');
}
function send() {
socket.emit('clientMessage', 'Yo server');
};
</script>
I managed to get connection and deconnection working but I'm unable to get the io.sockets.on('clientMessage' to work. Any ideas?
I'm not sure exactly how to understand the usage of message in socket.io. I'm trying to send message from the client to server then the server reply to the client (basic nah?).
This is the server code:
var fs = require('fs');
var hskey = fs.readFileSync('ssl.key');
var hscert = fs.readFileSync('ssl.crt');
var options = {
key: hskey,
cert: hscert
};
var app = require('https').createServer(options);
var io = require('/usr/local/lib/node_modules/socket.io').listen(app);
app.listen(8181);
io.sockets.on('connection', function (socket) {
socket.emit('serverMessage', 'Hello world!');
io.sockets.emit('serverMessage', 'A user connected');
io.sockets.on('clientMessage', function (socket) {
socket.emit('serverMessage', 'Gotamessage bitch!');
console.log('I received a message by ', from, ' saying ', msg);
});
socket.on('disconnect', function () {
io.sockets.emit('serverMessage', 'A user disconnected');
});
});
This is the client code:
<script>
var socket;
var firstconnect = true;
function connect() {
if(firstconnect) {
socket = io.connect('https://secure.connectednodes.:8181');
socket.on('serverMessage', function(data){ message(data); });
socket.on('connect', function(){ status_update("Connected to Server"); });
socket.on('disconnect', function(){ status_update("Disconnected from Server"); });
socket.on('reconnect', function(){ status_update("Reconnected to Server"); });
socket.on('reconnecting', function( nextRetry ){ status_update("Reconnecting in "
+ nextRetry + " seconds"); });
socket.on('reconnect_failed', function(){ message("Reconnect Failed"); });
firstconnect = false;
}
else {
socket.socket.reconnect();
}
}
function disconnect() {
socket.disconnect();
}
function message(data) {
document.getElementById('message').innerHTML += "<br>" + "Server says: " + data;
}
function status_update(txt){
document.getElementById('status').innerHTML = txt;
}
function esc(msg){
return msg.replace(/</g, '<').replace(/>/g, '>');
}
function send() {
socket.emit('clientMessage', 'Yo server');
};
</script>
I managed to get connection and deconnection working but I'm unable to get the io.sockets.on('clientMessage' to work. Any ideas?
Share Improve this question edited Mar 3, 2014 at 20:50 user1861458 asked Apr 11, 2012 at 1:05 exomicexomic 4866 silver badges17 bronze badges1 Answer
Reset to default 7I think you must use socket.on not io.sockets.on
Server side
socket.on('clientMessage', function (data, from) {
socket.emit('serverMessage', 'Got a message!');
console.log('I received a message by ', from, ' saying ', msg);
});
Client side
function send() {
socket.emit('clientMessage', 'Yo server', 'USER001');
};
try it