First we need a websocket server, before the post has done
Tomcat erecting a simple websocket server
When you use it, just open it, whatever it is.
Create a new scene in unity
Build UI (Ugui)
There is a connection button
An information input box Inputfield
A Send button
A disconnect button
A message box that displays the text
Create a gameobject in the scene, add a script on it, call Wsmgr.
Using the Besthttp plugin.
Using system.collections;using system.collections.generic;using unityengine;using besthttp;using BestHTTP.WebSocket ; using system;using besthttp.examples;using unityengine.ui;using system.text;public class Wsmgr:monobehaviour {//pub Lic string url = "Ws://localhost:8080/web1/websocket"; public string url = "Ws://localhost:8080/web1/ws"; Public Inputfield msg; public Text console; Private WebSocket WebSocket; private void Start () {init (); } private void Init () {webSocket = new WebSocket (new Uri (URL)); Websocket.onopen + = OnOpen; Websocket.onmessage + = onmessagereceived; Websocket.onerror + = OnError; websocket.onclosed + = onclosed; } private void Antiinit () {websocket.onopen = null; Websocket.onmessage = null; Websocket.onerror = null; websocket.onclosed = null; WebSocket = null; private void Setconsolemsg (String msg) {Console.text = "Message:" + msg; } public void Connect () {websocket.open (); } private byte[] GetBytes (String message) {byte[] buffer = Encoding.Default.GetBytes (message); return buffer; } public void Send () {websocket.send (msg.text); } public void Send (String str) {websocket.send (str); } public void Close () {websocket.close (); } #region WebSocket Event handlers//<summary>//called when the Web socket is open, and we were ready t o Send and receive data//</summary> void OnOpen (WebSocket ws) {Debug.Log ("connected"); Setconsolemsg ("Connected"); }//<summary>//Called when we received a text message from the server///</summary> void OnM Essagereceived (WebSocket ws, String message) {DEBUG.LOG (message); SETCONSOLEMSG (message); }//<summary>//called when the Web socket closed///</summary> void OnClosed (WebSocket ws, UInt16 Code, string message) {DEBUG.LOG (message); SETCONSOLEMSG (message); Antiinit (); Init (); } private void OnDestroy () {if (websocket!=null && websocket.isopen) {websocket.cl OSE (); Antiinit (); }}///<summary>//Called when a error occured on client side//</summary> void OnError (W Ebsocket WS, Exception ex) {string errormsg = string. Empty; #if! Unity_webgl | | Unity_editor if (ws. Internalrequest.response = null) ErrorMsg = string. Format ("Status Code from Server: {0} and Message: {1}", WS. InternalRequest.Response.StatusCode, WS. InternalRequest.Response.Message); #endif Debug.Log (errormsg); Setconsolemsg (ERRORMSG); Antiinit (); Init (); } #endregion}
The Connect Send Close three method corresponds to the function of three buttons
OnOpen OnMessage OnError OnClose These four are the websocket of the four events corresponding to the method
First, in the Start method, init ()
Click Connect,
Type in the input box and click Send
Click Close to disconnect the connection
A message is displayed in the text at the bottom
Similarly, the Tomcat server also has a display
Publish WebGL Tests available
Simple use of websocket in unity