Unity doesn't see the disconnect from the server when called by another Unity, any ideas?
For the first time, I am doing something networked, and now I decided to make friends between the Unity client and the Nodejs server. Subject: I run "play" in Unity - there is a connection to the server. I load a copy of this project through symbolic links, run "play" there - the second connection to the server takes place. In the serverObjects collection, 2 objects are seen, everyone is happy, on both clients everyone can see each other, the server fixes 2 connections. Then I disconnect one of the Unity projects (it doesn't matter by pressing "stop" or shutting down the entire project) - the server fixes the disconnect, but the remaining Unity does not react at all, as if the disconnect event from the server did not come at all. The last message from the "register" event is in the Unity log. "spawn" worked too. But here "rdisconnected" does not see at all, neither immediately, nor after a couple of minutes (I thought, maybe some kind of timeout, which I don't know about). Any assignment help appreciated. Question: are there any ideas on how to cure this? And along the way - if there is a suspicion that this is due to the fact that the check goes through symbolic links (that is, in fact, the same project, possibly with one ID or address or something else), then how to test network applications on Unity in general?
Code Nodejs:
var io = require('socket.io')(process.env.PORT || 52300);
// Custom classes
var Player = require('./Classes/Player.js');
console.log('Server has started');
var players = [];
var sockets = [];
io.on('connection', function(socket) {
console.log('Connection made!');
var player = new Player();
players[player.id] = player;
sockets[player.id] = socket;
// Tell the client that this is our id for the server
socket.emit('register', {
id: player.id
});
socket.emit('spawn', player); // Tell myself i have spawned
socket.broadcast.emit('spawn', player); // Tell others i have spawned
// Tell myself about everyone else in the game
for (var pID in players) {
if (pID !== player.id) {
socket.emit('spawn', players[pID]);
}
}
socket.on('disconnect', function() {
console.log('Player is disconnected');
socket.broadcast.emit('rdisconnected', player);
delete players[player.id];
delete sockets[player.id];
});
});
The code on the Unity client in the only component added to the scene:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SocketIO;
using Project.Utility;
namespace Project.Networking {
public class NetworkClient : SocketIOComponent {
[Header("Network Client")]
[SerializeField]
private Transform networkContainer;
private Dictionary<string, GameObject> serverObjects;
// Start is called before the first frame update
public override void Start() {
base.Start();
initialize();
setupEvents();
}
// Update is called once per frame
public override void Update() {
base.Update();
}
private void initialize() {
serverObjects = new Dictionary<string, GameObject>();
}
private void setupEvents() {
On("open", (E) => {
Debug.Log("Connection made to the server");
});
On("register", (E) => {
string id = E.data["id"].ToString().RemoveQuotes();
Debug.LogFormat("Our Client's ID ({0})", id);
});
On("spawn", (E) => {
// Handling all spawning players
string id = E.data["id"].ToString().RemoveQuotes();
GameObject go = new GameObject("Server ID: " + id);
go.transform.SetParent(networkContainer);
serverObjects.Add(id, go);
});
On("rdisconnected", (E) => {
string id = E.data["id"].ToString().RemoveQuotes();
Debug.LogFormat("User {id} disconnected", id);
//Destroy(serverObjects[id]); // Remove from game
//serverObjects.Remove(id); // Remove from memory
//Debug.LogFormat("Dictionary has {count} objects", serverObjects.Count);
});
}
}
}
Your answer
Follow this Question
Related Questions
Unity3d and Node.js backend 2 Answers
Player falls through terrain walls? 0 Answers
Drawing a GUI line 0 Answers
[2D] Getting the y coordinate of the top of a game object 0 Answers
How to stop AR camera shaking? 0 Answers