Closed tho1294 closed 2 years ago
This is one of the ways, you can put the SocketManager into the Camera object (in one scene, the home/first one for the example, or the one after login, don't do it (putting the SocketManager/Script to all cameras of all scenes), then use DontDestroyOnLoad
at the initiation
void Start() // or Awake()
{
if(socket == null){ // or if(!socket)
StartSocketConnection();
DontDestroyOnLoad(transform.root.gameObject);
}
}
void StartSocketConnection()
{
var uri = new Uri("https://example.com");
socket = new SocketIOUnity( ...
...
}
I put only one script in my scene, not on camera i try with the DontDestroyOnLoad too but i have the same issue
i can send you my code if you want
Somehow you called Connect or ConnectAsync multiple times
socket = new SocketIOUnity...;
// line bellow should be called one time in the runtime or one time before Disconnecting it
socket.Connect();
private void connectToSignallingServer()
{
var uri = new Uri("http://192.168.0.15:3000");
socket = new SocketIOUnity(uri, new SocketIOOptions
{
EIO = 4,
Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
});
socket.On("connect", (response) =>
{
Debug.Log("connectToSignallingServer: connect");
socket.Emit("create or join", "foo");
});
socket.On("ipaddr", (response) => { Debug.Log("connectToSignallingServer: ipaddr"); });
socket.On("created", (response) =>
{
Debug.Log("connectToSignallingServer: created");
isInitiator = true;
});
socket.On("full", (response) => { Debug.Log("connectToSignallingServer: full"); });
socket.On("join", (response) =>
{
Debug.Log("connectToSignallingServer: join");
Debug.Log("connectToSignallingServer: Another peer made a request to join room");
Debug.Log("connectToSignallingServer: This peer is the initiator of room");
isChannelReady = true;
});
socket.On("joined", (response) =>
{
Debug.Log("connectToSignallingServer: joined");
isChannelReady = true;
});
socket.On("log", (response) =>
{
//TODO
/*for (Object arg : response) {
Debug.Log( "connectToSignallingServer: " + String.valueOf(arg));
}*/
});
socket.On("message", (response) => { Debug.Log("connectToSignallingServer: got a message"); });
socket.On("message", (response) => {Debug.Log("connectToSignallingServer: got a message"); });
socket.On("disconnect", (response) => { Debug.Log("connectToSignallingServer: disconnect"); });
socket.Connect();
}
This is one of the ways, you can put the SocketManager into the Camera object (in one scene, the home/first one for the example, or the one after login, don't do it (putting the SocketManager/Script to all cameras of all scenes), then use
DontDestroyOnLoad
at the initiationvoid Start() // or Awake() { if(socket == null){ // or if(!socket) StartSocketConnection(); DontDestroyOnLoad(transform.root.gameObject); } } void StartSocketConnection() { var uri = new Uri("https://example.com"); socket = new SocketIOUnity( ... ... }
this should do the job, if you are testing in the Editor.. this is a Unity Editor issue, pause or stop the viewport (preview) doesn't stop the socket connection until you kill the editor ... test directly into a pc/mac app.
i try all of that i try your sample i build / restart my computer but i don't understand why i continue to investigate and i tell you if i found a solution
i forgot to mention that the
socket
object should be staticpublic static SocketIOUnity socket = null;
so the code should be like this
public class SocketManager : MonoBehaviour { ... public static SocketIOUnity socket = null; void Start() // or Awake() { if(SocketManager.socket == null){ // or if(!SocketManager.socket) StartSocketConnection(); DontDestroyOnLoad(transform.root.gameObject); } } void StartSocketConnection() { var uri = new Uri("https://example.com"); SocketManager.socket = new SocketIOUnity( ... ... } ...
Multiple connections = you called socket.Connect()
StartSocketConnection()
or connectToSignallingServer()
method multiple times, and avoid testing your game app using the Editor.
yes i put log in StartSocketConnection and it's call one time i try with build on android and on windows
I'll close this issue as I feel it has been resolved. Feel free to comment again if this is not the case and we can reopen the issue.
Hello i have a problem when i use your plugin i have a double connection on my server do you have any idea to fix that?