Web Sockets for Unity
For Unity developers looking to use Web Sockets in their Unity game / app.
First download the required dependencies and extract the contents into your Unity project "Assets" folder.
- IWebSocket interface for targeting the various platforms Unity supports.
|ConfigureWebSocket(url)||Configures web socket with url and optional headers|
|ConnectAsync()||Connect to web socket|
|CloseAsync()||Close web socket connection|
|IsOpen()||Check if web socket status is open|
|Url()||Return the URL being used by the web socket|
Interface event delegates
OnError(object sender, WebSocketErrorEventArgs e); OnOpen(object sender, EventArgs e); OnMessage(object sender, WebSocketMessageEventArgs e); OnClose(object sender, WebSocketCloseEventArgs e);
UnityWebSocketDemo project repo contains sample scenes showing how to hook all this up in the Unity Editor.
Other developer notes
When using Unity 2017.2.1p2 and the .NET 4.6 API (Experimental) player settings I tried the system ClientWebSocket. While this initially had some success the problem was that after 3 mins or so the following error would occur on the async/await connect function in the Unity Editor:
ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream'
Therefore I opted for WebSocket-Sharp which targets .NET Framework 3.5 and works in the Unity 2017 editor and doesn't seem to have the same issue.
Questions or tweet @deadlyfingers