使用插件:besthttp 1.10
u3d版本:5.4.0
代碼:找的網上的一篇代碼,自己偷懶沒寫 只簡單測試了下
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 { //public string url = "ws://localhost:8080/web1/websocket"; [SerializeField] private string url; public InputField msg; public Text console; private WebSocket webSocket; private void Start() { init(); print("執行"); //WebSocket1(); } void Update() { print(webSocket.IsOpen); } void WebSocket1() { WebSocket webSocket = new WebSocket(new Uri(url)); //webSocket.OnOpen... //注冊事件之后,我們就可以開始連接 webSocket.Open(); //在這一步之后,我們將收到一個OnOpen事件,我們可以開始發送消息到服務器 webSocket.Send("Message to the Server"); byte[] buffer = new byte[1024]; webSocket.Send(buffer); //所有通信完成之后,就關閉連接 webSocket.Close(); } private void init() { webSocket = new WebSocket(new Uri(url)); webSocket.OnOpen += OnOpen; webSocket.OnMessage += OnMessageReceived; webSocket.OnError += OnError; webSocket.OnClosed += OnClosed; webSocket.Open(); print("執行這里"); } 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(); print(webSocket!=null); } 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 are ready to 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 OnMessageReceived(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 + "close"); // setConsoleMsg(message); //antiInit(); // init(); } //void OnApplicationQuit void OnApplicationQuit() { //print(webSocket.IsOpen); //if (webSocket != null && webSocket.IsOpen) //{ print("quit"); // webSocket.Close(); // //antiInit(); //} } private void OnDestroy() { print("destory"); //if (webSocket != null && webSocket.IsOpen) //{ // print("destory"); // webSocket.Close(); // //antiInit(); //} } /// <summary> /// Called when an error occured on client side /// </summary> void OnError(WebSocket 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 }
腳本中對應的方法填入到對應的button中,執行,不需要切換成webgl直接PC端測試能用,
服務端測試地址:http://ruyice.com/test 我是在這個網址中的websocket進行測試的,不用自己麻煩的搭建服務端
在腳本中的url中填寫上圖中的測試ws網址:ws://demos.kaazing.com/echo
測試結果:
能運行,測試過程中出現的問題,如果在OnDestory()和OnApplicationQuit()中使用websocket.close()的話就會報錯,雖然不影響什么,但是總看着不舒服,所以直接把close方法去掉了,程序關掉的同時也關掉了websocket的,就這樣了