Skip to content

Instantly share code, notes, and snippets.

@clucle
Last active October 12, 2017 13:09
Show Gist options
  • Save clucle/3eff1cc6ff8e4faf1d912af8d3a3be4b to your computer and use it in GitHub Desktop.
Save clucle/3eff1cc6ff8e4faf1d912af8d3a3be4b to your computer and use it in GitHub Desktop.
Unity WebSockSharp Client
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