-
Import UnityWebSocket.unitypackage in Unity, Require:
* Require Scripting Runtime Version = .Net 4.x * Require WebGL LinkerTarger = asm.js or Both -
Easy to use WebSocket
// the namespace using UnityWebSocket; // create instance WebSocket scoket = new WebSocket(); // register callback scoket.OnOpen += OnOpen; scoket.OnClose += OnClose; scoket.OnMessage += OnMessage; socket.OnError += OnError; // connect string address = "ws://echo.websocket.org"; socket.ConnectAsync(address); // send data (tow ways) socket.SendAsync(str); // send String data socket.SendAsync(bytes); // send byte[] data // close connection socket.CloseAsync();
-
more detail usage, see the Example.cs code in project。
-
there are many WebSocket class in different namespace, use in different situations.
-
WebSocket.jslib, syntax follow to asm.js。
Path: Plugins/WebGL/WebSocket.jslib Fucntion:Unity will deploy it to web js runtime on WebGL platform. -
Example Scene
Function: Example how to use UnityWebSocket.
- use Official Echo Test Server. refer to Echo Test.
