Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DavidMonk · Apr 23, 2021 at 08:51 AM · unity5nodejs

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);
       });
     }
   }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

180 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges