Unable to join 2 sockets to rooms simultaneously - socket.io

I'm traying to billed a 2-playerg game using Socket.io to communicate between the opponents.
The basic idea is that whenever a socket is connected is will be saved in a global variable (and the front-end will wait for opponents), and when another socket is connected - booth of them will join a room and the game will start.
Within the "start game" function - only socket1 (the second user to log in) is actually joining the room.
what am I missing?
let watingUser = undefined;
io.on('connection', (socket) => {
console.log(socket.handshake.query.name + ' has connected');
handelNewUser(socket);
socket.on('disconnect', () => {
console.log(socket.handshake.query.name + ' has disconnected');
});
socket.on('move', (pitNum, room) => {
socket.to(room).emit('opponent-move', pitNum);
});
});
const handelNewUser = (socket) => {
socket.emit('login', {
id: socket.id,
});
if (watingUser) startGame(socket, watingUser);
else waitForOpponent(socket);
};
const startGame = (socket1, socket2) => {
watingUser = undefined;
console.log('wating room is empty');
const room = uniqid();
socket1.join(room);
socket2.join(room);
socket1.to(room).emit('game-start', {
room,
opponent: socket1.handshake.query.name,
});
socket2.to(room).emit('game-start', {
room,
opponent: socket2.handshake.query.name,
});
console.log('gameStart');
};
const waitForOpponent = (socket) => {
watingUser = socket;
console.log(watingUser.handshake.query.name + ' is wating');
};

Related

socket.io buffered messages loss after reconnection

This is on client side
socket.on('connect', () => {
console.log('client connect', socket.id);
const token = getToken();
socket.emit('token', token);
});
socket.on('message', data => {
....
//handle message
});
This is on server side
io.on('connection', (client) => {
client.on('token', token => {
verifyToken(token)
.then(({ _id: clientId }) => {
if (!clientId) return;
if (!connections[clientId]) {
connections[clientId] = new Map();
}
connections[clientId].set(client, 1);
client.on('disconnect', () => {
connections[clientId].delete(client);
});
});
});
});
}
async sendMessageToClients (workspaceId, message) {
const workspace = await getWorkspaceQuery(workspaceId);
if (!workspace) return;
const workspaceMembers = workspace.members.map(({ user }) => user);
for (const memberId of workspaceMembers) {
if (connections[memberId]) {
for (const clientConnection of connections[memberId].keys()) {
console.log('send to client', memberId, message.content, clientConnection.connected, clientConnection.id);
clientConnection.emit('message', message);
}
}
}
};
}
I purposely make a client offline by disconnect the wifi connection (make it in offline mode), what happen is that
a. if the disconnection is short, socket.id stay the same and I can get the buffered message send by other client when comes online;
b. but if I the disconnection is longer, the socket.id will change, and I can't get the buffered message send by other client when comes online.
How should I address that?
Since according to here the messages should be ideally buffered after reconnection.

Notify specific user

I'm trying to make a notification system, but I don't know why the code not working... The redis shoots the event, and the event is received... On the "node server.js in CMD" returns:
C:\xampp\htdocs\test>node socket.js
Listening on Port 3000
{"message":"teste","user":1} //returns when publish redis in controller.
Controller:
public function fire(){
$redis = LRedis::connection();
$data = ['message' => 'teste', 'user' => 1];
if($redis->publish('message', json_encode($data))){
return response()->json(['sucesss' => 1]);
} else {
return response()->json(['error' => 0]);
}
}
Template.blade.php:
<script>
var socket = io('http://localhost:3000');
//console.log(socket); IS CONNECTED...
socket.on('message', function (data) {
console.log(data);
});
</script>
socket.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('message', function(err, count) {
});
redis.on('message', function(channel, message) {
console.log(message);
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
http.listen(3000, function(){
console.log('Listening on Port 3000');
});
Console.log
Returns nothing...
If you help me to make work, how I can notify specific user? Example, user 1 creat a link, a link is commented by user 2, the user 1 be notified...

socket io unique channel - emit without specifying the room

On my project each user is joining his unique channel (I know that the user is also in a socked id channel).
Client
var socket = io('http://localhost:8000/');
var room = "unique_id";
socket.on('connect', function () {
socket.emit('room', room);
});
socket.on("unique_id", function() {
console.log("test");
})
On the server side I am checking the rooms a user is connected to:
var io = require('socket.io')(8000);
io.on('connection', function(socket) {
var room;
socket.on('room', function(unique_id) {
socket.join(unique_id);
room = unique_id;
console.log(io.sockets.adapter.rooms); //show the unique_id room
io.to(room).emit('hello', 'msg');
});
socket.on(room, function(data) {
console.log(room);
})
});
Output on connect
{ 'DQOt2DX2IsHh-m9nAAAC': { sockets: { 'DQOt2DX2IsHh-m9nAAAC': true }, length: 1 },
'unique_id': { sockets: { 'DQOt2DX2IsHh-m9nAAAC': true }, length: 1 } }
Now I am using elephant.io to emit to the client as follows
$client = new Client(new Version1X('http://localhost:8000'));
$client->initialize();
$client->emit('unique_id', ['news' => 'bar']);
$client->close();
I think there is something wrong on my server side but I am not able to solve it. I need to process the events for the unique_id but how?
server side
var io = require('socket.io')(8000);
io.on('connection', function(socket) {
var room;
socket.on('room', function(address) {
socket.join(address);
room = address;
console.log(io.sockets.adapter.rooms);
io.to(room).emit('hello', 'msg');
});
socket.on('message', function(data) {
console.log(data.unique_id);
io.to(data.unique_id).emit('hello', 'msg');
})
});
elephant.io
$client->emit('message', ['unique_id' => 'foo']);
This is my solution which is working for me.

node.js - Socket IO times out after some time

I'm trying to build up a socket.io server for my own multiplayer game, but for some reason the server goes down after a certain amount of time and I don't know why. I have tried several ways to run the server (nodemon and forever, everything with or without screen). I don't think that this is an inactivity problem because I added a random stuff generator to simulate some activity on the server, yet the problem persists. My cpu load with the running server stays between 2-3 %. I'm running node 4.x and the current stable socket.io build (1.3.6).
And here is my code:
var shortId = require('shortid'),
io = require('socket.io')(process.env.PORT || 4567),
mysql = require('mysql'),
connection = mysql.createConnection({
host: 'localhost',
user: 'xxx',
password: 'xxx',
database: 'xxx'
});
var clients = [];
var clientLookup = {};
//Placed Components (builded stuff from players or enviroment)
var placed_components = 'Server1_PlacedC';
connection.connect(function (err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
});
setInterval(function () {
var random = Math.random();
//connection.query(
// 'INSERT INTO '+placed_components+' SET ?',
// {data:countdown},
// function(err,result){
// if (err) throw err;
// }
//);
console.log(random);
}, 100);
io.on('connection', function (socket) {
var currentClient;
console.log('connected', socket.id);
//////////////////////////////////////////////////////////////////////////////
socket.on('disconnect', function () {
console.log('player disconnected', currentClient.id);
socket.broadcast.emit('disconnected', currentClient)
var index = clientLookup[currentClient.id];
clients.splice(index, 1);
})
///////////////////////////////////////////////////////////////////////////// /
socket.on('register', function (data) {
currentClient = {
id: shortId.generate(),
health: 100,
isDead: false
};
console.log('registering', currentClient.id);
clients.push(currentClient);
clientLookup[currentClient.id] = currentClient;
socket.emit('registerSuccess', {id: currentClient.id});
socket.broadcast.emit('spawn', {id: currentClient.id});
console.log('connected players', clients.length);
clients.forEach(function (client) {
if (currentClient.id == client.id)
return;
socket.emit('spawn', {id: client.id});
console.log('sending spawn to new player for playerid', client.id);
})
});
socket.on('beep', function () { // Beep Request
socket.emit('boop');
console.log("received some beep!");
});
socket.on('move', function (data) {
data.id = currentClient.id;
socket.broadcast.emit('move', data);
//console.log(JSON.stringify(data));
});
socket.on('ShareObject', function (data) {
data.id = currentClient.id;
socket.broadcast.emit('ReveiveObject', data);
console.log(JSON.stringify(data));
});
socket.on('SharePlayerAnimation', function (data) {
data.id = currentClient.id;
socket.broadcast.emit('BroadcastPlayerAnimation', data);
console.log("a Player changed his animation" + JSON.stringify(data));
});
//////////////////////////////////////////////////////////////////////////////
socket.on('benchmark', function (data) {
console.log(data);
});
//////////////////////////////////////////////////////////////////////////////
})
console.log('server started');

handle browser refresh socket.io

I have a requirement using node js that handles disconnecting a user from a chat application.
I am not sure how to handle telling the difference between a browser closing and a user refreshing the browser.
client.on('disconnect', function () {
console.log( 'Disconnected' );
// run mysql code to remove user from logged in table
});
I have googled for a couple hours and cannot find a solution.
This seems like something pretty simple and I think it is the keywords that I am using.
Can someone point me in the right direction on how to handle this?
Thanks in advance.
One way would be to generate a random UID and save it to local storage. Right after the client connects, send this UID to the server and check to see if that UID exists as a connected user. On the server side, set a timeout in the disconnect that gives the user 15 seconds or so before their unique UID is deleted from the "users online" data.
Client:
// When the client starts, create the uid.
localstorage.setItem('uUID', Math.random().toString(24) + new Date());
// Emit the UID right after connection
socket.emit('userLogin', localstorage.getItem('uUID');
Server:
var currentUIDS = [];
var userIsConnected = true;
io.sockets.on('connection', function (socket) {
var currentUID = null;
socket.on('userLogin', function (data) {
if (data !== null) {
if (currentUIDS.includes(data)) {
userIsConnected = true;
currentUID = data;
}
}
});
socket.on('disconnect', function () {
userIsConnected = false;
setTimeout(function () {
if (!userIsConnected) currentUIDS.pop(currentUID);
}, 15000);
});
});
I have a better solution for that to handle multiple users:
var users = [],
users_connected = [];
io.on('connection', function(socket) {
var uid = null;
// register the new user
socket.on('register', function (user_uid) {
if ( users_connected.indexOf(user_uid) < 0 ) {
users_connected.push(user_uid);
}
if ( users.indexOf(user_uid) < 0 ) {
console.log('New user connected: ' + user_uid);
users.push(user_uid);
// notify other clients that a new user has joined
socket.broadcast.emit('user:join', {
name: user_uid,
users: users_connected.length
});
}
uid = user_uid;
});
// clean up when a user leaves, and broadcast it to other users
socket.on('disconnect', function () {
users_connected.splice( users_connected.indexOf(uid), 1);
setTimeout(function () {
if ( users_connected.indexOf(uid) < 0 ) {
socket.broadcast.emit('user:left', {
name: uid
});
var index = users.indexOf(uid);
users.splice(index, 1);
}
}, 3000);
});
});

Resources