Last active
October 12, 2017 13:09
-
-
Save clucle/3eff1cc6ff8e4faf1d912af8d3a3be4b to your computer and use it in GitHub Desktop.
Unity WebSockSharp Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using WebSocketSharp; | |
using UnityEngine; | |
public class webSockManager : MonoBehaviour { | |
public static webSockManager instance = null; | |
WebSocket ws = null; | |
void Awake() { | |
if (instance == null) | |
instance = this; | |
else if (instance != this) | |
Destroy (gameObject); | |
DontDestroyOnLoad (gameObject); | |
InitSocket (); | |
} | |
void InitSocket () { | |
ws = new WebSocket ("ws://localhost:10001"); | |
ws.OnMessage += (sender, e) => | |
ReceiveMessage (e.Data); | |
ws.Connect (); | |
if (ws.IsAlive) | |
Debug.Log ("Web Socket Connect"); | |
else | |
Debug.Log ("Web Socket not Connect"); | |
} | |
void OnDestroy() { | |
ws.Close (); | |
} | |
public bool SendMessage(string msg) { | |
if (!ws.IsAlive) | |
return false; | |
ws.Send (msg); | |
return true; | |
} | |
private void ReceiveMessage(string msg) { | |
Debug.Log (msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment