首先我們需要一個websocket服務(wù)器,之前的博文中有做
Tomcat架設(shè)簡單Websocket服務(wù)器
用的時候打開就行了,先不管它
Unity中新建場景
建UI(UGUI)
有一個連接按鈕Button
一個信息輸入框InputField
一個發(fā)送按鈕Button
一個斷開按鈕Button
一個消息顯示框Text
(猜你喜歡:Unity 連接WebSocket(ws://)服務(wù)器的方法)
場景中建一個GameObject,在上面加個腳本,就叫WSMgr好了
用到了BestHTTP這個插件
(猜你喜歡:在Unity3d下如何使用WebSocket)
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";
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
///
/// Called when the web socket is open, and we are ready to send and receive data
///
void OnOpen(WebSocket ws)
{
Debug.Log("connected");
setConsoleMsg("Connected");
}
///
/// Called when we received a text message from the server
///
void OnMessageReceived(WebSocket ws, string message)
{
Debug.Log(message);
setConsoleMsg(message);
}
///
/// Called when the web socket closed
///
void OnClosed(WebSocket ws, UInt16 code, string message)
{
Debug.Log(message);
setConsoleMsg(message);
antiInit();
init();
}
private void OnDestroy()
{
if(webSocket!=null && webSocket.IsOpen)
{
webSocket.Close();
antiInit();
}
}
///
/// Called when an error occured on client side
///
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
}
Connect Send Close 三個方法對應(yīng)的就是三個按鈕的功能
OnOpen OnMessage OnError OnClose這四個一看就是Websocket的四個事件對應(yīng)的方法
首先在Start方法中init()
點(diǎn)擊Connect,連接
在輸入框中輸入,點(diǎn)擊Send就發(fā)送
點(diǎn)擊Close斷開連接
底部的Text中會顯示消息
同樣,Tomcat服務(wù)端也有顯示
發(fā)布WebGL測試可用
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。