FlerrElectronics
FlerrElectronics

Reputation: 291

socket.io-client connecting, but not emitting

I am making a little encrypted chat app, in the terminal, using socket.io-client and socket.io. The client is able to connect to the server, but is not emitting the username, when its entered.

Client:

 var socket = require('socket.io-client')('http://127.0.0.1:3000');
 socket.on('connect_error', function(){
console.log('Failed to establish a connection to the servers, or lost     connection');

 return process.exit();     
  });
 var prompt = require("prompt-sync")()
 var news = "Add news: Will be from database. " 
 var username = prompt("Username>: ")

 console.log("Hold on a sec, just checking that!")
 console.log("")
 if (typeof username === "defined"){
 socket.emit('user-name', {usr: 'username'})
 }


socket.on('user-name-good',function(socket){
console.log("Okay! Your username looks good, we just require your password")
console.log("If you chose to have no password, please press enter with    out pressing space!")
 var password = prompt("Password>: ")
  if (typeof password !== "defined"){
    console.log("Please provide a password!")
    return password = prompt("Username>: ")
 }

 socket.on('user-name-fail',function(socket){
 console.log("Sorry, we could not find, "+username+""+"Please register    on the website, or, if you have registered ")
 return process.exit()
})    

}
)

Server code, is based on code from socket.io chat example:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});

http.listen(3000, function(){
console.log('listening on *:3000');
});

I have added a error event, this closes the client if a connection to the server fails, so I know its connecting, any help appreciated, I have done research on this topic, and tried a lot of other methods, but to no avail.

Also the connection is made after you submit data, not when the client code is started, what could be causing this?

Upvotes: 0

Views: 5572

Answers (1)

Azamantes
Azamantes

Reputation: 1461

If you want to send events between client and server you have to:

Send event A from client to the server and server has to be listening for the A event.

If you want to send event B from server to client then client has to be listening for the event B.

Apart from everything else in your code I don't see where you are listening for the 'chat message' event on the client side.

Socket.io is based on these so called 'events'. The code below is going to send 'my_event' event to the server and the trasmitted data is going to be the object { a: 1 }.

socket.emit('my_event', { a: 1 });

If I want to handle this event on the server I have to listen for it:

socket.on('my_event', function(data) {
    // data is the object { a: 1 }
    // do stuff..
});

Upvotes: 1

Related Questions