nhn / socket.io-client-unity3d

socket.io-Client for Unity3D, which is compatible with socket.io v1.x
Other
167 stars 43 forks source link

Emit Issues #11

Closed farzadi closed 7 years ago

farzadi commented 7 years ago

How can we Emit outside Start()? I try all of the ways to emit in other methods but it's not working could you please give me an example this is the way I use socket

static Socket socket; void Start() {
socket = Socket.Connect(_networkUrl); }

public void JoinToGame() { socket.Emit("joinToGame", RequestForGame(DB._myId, _myRoom).ToString()); }

ppz0th commented 7 years ago

@farzadi Socket is not connected just after you call Connect() method. It's not blocking call so you have to catch "connect" event. "connect" event means that the connection between with your socket and server's socket is ready to exchange packets.

You can change above code like this manner

static Socket socket; static bool isSocketConnected = false;

void Start() { socket = Socket.connect(_networkUrl); socket.On("connect", () => { isSocketConnected = true; }); StartCoroutine(JoinToGame); }

IEnumerator JoinToGame() { yield return new WaitUntil(() => isSocketConnected ); socket.Emit("joinToGame", RequestForGame(DB._myId, _myRoom).ToString()); }

farzadi commented 7 years ago

Thank you worked!