Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.get() AND socket.set() gives problems

I'm using NodeJs v0.10.28 and everytime i try to login to the chat i get a error

TypeError: Object #<Socket> has no method 'set' at Socket.<anonymous>

And if i'm deleting the lines it works but not working right.

What is the wrong thing in this code

// get the name of the sender
socket.get('nickname', function (err, name) {
  console.log('Chat message by ', name);
  console.log('error ', err);
  sender = name;
}); 

AND

socket.set('nickname', name, function () {
  // this kind of emit will send to all! :D
  io.sockets.emit('chat', {
    msg : "Welcome, " + name + '!', 
    msgr : "Nickname"
  });
});

FULL CODE

http://pastebin.com/vJx7MYfE

like image 344
Hakan Köse Avatar asked Jun 01 '14 00:06

Hakan Köse


People also ask

Why Socket is not connected?

The error message “ERR_SOCKET_NOT_CONNECTED” is instantly solved in the majority of the cases when we flush the sockets on your browser. This will break the connection between any active pages on your browser and you might have to reinitialize everything.

How do I reject a Socket.IO connection?

You can either use socket. close() or socket. terminate() to close the connection.

Can Socket.IO connect to WebSocket server?

Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.


2 Answers

You have to set the nickname property directly in the socket!

From socket.io website:

The old io.set() and io.get() methods are deprecated and only supported for backwards compatibility. Here is a translation of an old authorization example into middleware-style.

Also, from an example of the socket.io website :

// usernames which are currently connected to the chat
var usernames = {};
var numUsers = 0;

    // when the client emits 'add user', this listens and executes
  socket.on('add user', function (username) {
    // we store the username in the socket session for this client
    socket.username = username;
    // add the client's username to the global list
    usernames[username] = username;
    ++numUsers;
    addedUser = true;
    socket.emit('login', {
      numUsers: numUsers
    });
    // echo globally (all clients) that a person has connected
    socket.broadcast.emit('user joined', {
      username: socket.username,
      numUsers: numUsers
    });
  });

Check the example here : https://github.com/Automattic/socket.io/blob/master/examples/chat/index.js

like image 100
Ludovic C Avatar answered Nov 11 '22 04:11

Ludovic C


Like what Ludo said, set the nickname property directly in the socket:

// get the name of the sender
var name = socket.nickname;
console.log('Chat message by ', name);
console.log('error ', err);
sender = name;

and

// this kind of emit will send to all! :D
socket.nickname = name;
io.sockets.emit('chat', {
  msg : "Welcome, " + name + '!', 
  msgr : "Nickname"
});
like image 28
Brian Avatar answered Nov 11 '22 02:11

Brian