Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
WoodenMan/WoodenMan/Assets/Scripts/WebSocketClient.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
72 lines (63 sloc)
1.77 KB
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; | |
using UnityEngine; | |
using WebSocketSharp; | |
public class WebSocketClient | |
{ | |
WebSocketSharp.WebSocket m_ws; | |
public string m_Message; | |
bool m_isWSConnected = false; | |
// Connect WebSocket | |
void ConnectWS() | |
{ | |
if (m_isWSConnected) | |
return; | |
m_Message = "No data."; | |
//using (m_ws = new WebSocket("ws://localhost:8080/")) | |
using (m_ws = new WebSocket("ws://139.19.40.35:8080/")) | |
{ | |
//m_ws.Log.Level = WebSocketSharp.LogLevel.TRACE; | |
//m_ws.Log.File = "D:\\ws_log.txt"; | |
m_ws.OnOpen += (sender, e) => | |
{ | |
m_ws.Send(String.Format("Hello server.")); | |
Debug.Log("Connection opened."); | |
m_isWSConnected = true; | |
}; | |
m_ws.OnMessage += (sender, e) => | |
{ | |
m_Message = e.Data; | |
//Debug.Log(m_Message); | |
}; | |
m_ws.OnClose += (sender, e) => | |
{ | |
m_ws.Connect(); // This is a hack, but whatever | |
m_isWSConnected = false; | |
}; | |
m_ws.OnError += (sender, e) => | |
{ | |
// NOT PRINTING ERRORS | |
//Debug.LogError(e.Message); | |
m_isWSConnected = false; | |
}; | |
m_ws.Connect(); | |
} | |
} | |
// Use this for initialization | |
public void Start() | |
{ | |
ConnectWS(); | |
} | |
// Update is called once per frame | |
public void Update() | |
{ | |
//Debug.Log(m_Message); | |
//// Parse message | |
//if (m_InputParser != null) | |
// m_InputParser.parse(m_Message); | |
} | |
public void OnApplicationQuit() | |
{ | |
if (m_ws != null && m_ws.ReadyState == WebSocketState.OPEN) | |
m_ws.Close(); | |
} | |
} |