Socket.IO Authentication

Socket.IO Authentication
0.0 0

#1

Hi there…
I am using cocos2d-x socket.io and want to pass an argument as query or id on the start of connection but I think I have no any way to pass it!
for more detail suppose that I want to pass my chat room Id or …
is there any way to pass it?

auto _sioClient = SocketIO::connect(“MyHost:8080”, *this);
//you may set a tag for the client for reference in callbacks
_sioClient->setTag(“111”);
//_sioClient->emit("","{“msg_id”:“123”}");
//register event callbacks using the CC_CALLBACK_2() macro and passing the instance of the target class
_sioClient->on(“testevent”, CC_CALLBACK_2(SocketIOManager::testevent, this));
_sioClient->on(“echotest”, CC_CALLBACK_2(SocketIOManager::echotest, this));
_sioClient->on(“connect”, CC_CALLBACK_2(SocketIOManager::connect, this));
_sioClient->on(“disconnect”, CC_CALLBACK_2(SocketIOManager::disconnect, this));
_sioClient->on(“message”, CC_CALLBACK_2(SocketIOManager::message, this));
_sioClient->on(“json”, CC_CALLBACK_2(SocketIOManager::json, this));


#2

Hi Hadi,

For creating chat rooms, the best practice is to create new endpoints or “namespaces” on the server instead of using the default “/” namespace for everything. You can see the official documentation here:

https://socket.io/docs/rooms-and-namespaces/

For example, to create a chat room with the id “111”, you would use the following code on the server:

var nsp = io.of(’/111’);
nsp.on(‘connection’, function(socket){
console.log(‘someone connected’);
});

and then in the cocos2dx client, you would use the following code to connect to that chat room:

auto _sioRoomClient = SocketIO::connect(“MyHost:8080/111”, *this);

This would ensure that messages are only passed to that certain chat room / endpoint / namespace. In order to pass other information, the best method would be to encode it into json and sent it with an emit:

sioRoomClient ->emit("","{“query_id”:“123”,“another_argument”:“data”}");

Good luck!


#3

thanks a lot…I will try it as soon as I can…thanks and good luck…


#4

another problem…
I don’t know why I can’t connect my socket.io connection on my NIC loop back (127.0.0.1:3000)?!
I can run it on my client to real server, but when I want to run it on my local server, it closes the connection immediately(disconnects socket connection)!!
this is the exception inside WebSocket.cpp that caused to close the connection!
WS_MSG_TO_UITHREAD_CLOSE
also this is my connection code:

auto _sioClient = SocketIO::connect(“http://127.0.0.1:3000”, *this);
//you may set a tag for the client for reference in callbacks
_sioClient->setTag(“111”);
//register event callbacks using the CC_CALLBACK_2() macro and passing the instance of the target class
_sioClient->on(“testevent”, CC_CALLBACK_2(SocketIOManager::testevent, this));
_sioClient->on(“echotest”, CC_CALLBACK_2(SocketIOManager::echotest, this));
_sioClient->on(“connect”, CC_CALLBACK_2(SocketIOManager::connect, this));
_sioClient->on(“disconnect”, CC_CALLBACK_2(SocketIOManager::disconnect, this));
_sioClient->on(“message”, CC_CALLBACK_2(SocketIOManager::message, this));
_sioClient->on(“json”, CC_CALLBACK_2(SocketIOManager::json, this));
_sioClient->on(“clear”, CC_CALLBACK_2(SocketIOManager::clearMsg, this));

and this is my node.js file called app.js

var app = require(‘http’).createServer(handler),
io = require(‘socket.io’).listen(app),
static = require(‘node-static’); // for serving files

// This will make all the files in the current folder
// accessible from the web
var fileServer = new static.Server(’./’);

// This is the port for our web server.
app.listen(3000,‘127.0.0.1’);

// If the URL of the socket server is opened in a browser
function handler (request, response) {

    request.addListener('end', function () {
    //fileServer.serve(request, response);
});

}
io.on(‘connection’, function(socket){
console.log(‘A user connected’);
});

also this is my package.json (Node.js config settings):

{
“name”: “test_project”,
“version”: “0.0.1”,
“description”: “learn Socket.IO”,
“scripts”: {
“start”: “node app”,
“test”: “echo “Error: no test specified” && exit 1”,
},
“author”: “Hadi Abbasi”,
“license”: “ISC”,
“dependencies”: {
“net” : “",
“express” : "
”,
socket.io” : “*”
}
}

for running just I run this command: <node.js project path>nodemon app.js(now server is listenning but I can’t connect it, just server can recognize that a user is connecting it, then the connection will be closed!)