116 lines
4.1 KiB
C#
116 lines
4.1 KiB
C#
#if !UNITY_EDITOR && UNITY_WEBGL
|
|
using System;
|
|
|
|
namespace UnityWebSocket
|
|
{
|
|
public class WebSocket : IWebSocket
|
|
{
|
|
public string Address { get; private set; }
|
|
public WebSocketState ReadyState { get { return (WebSocketState)WebSocketManager.WebSocketGetState(instanceId); } }
|
|
|
|
public event EventHandler<OpenEventArgs> OnOpen;
|
|
public event EventHandler<CloseEventArgs> OnClose;
|
|
public event EventHandler<ErrorEventArgs> OnError;
|
|
public event EventHandler<MessageEventArgs> OnMessage;
|
|
|
|
internal int instanceId = 0;
|
|
|
|
public WebSocket(string address)
|
|
{
|
|
instanceId = WebSocketManager.AllocateInstance(address);
|
|
Log($"Allocate socket with instanceId: {instanceId}");
|
|
Address = address;
|
|
}
|
|
|
|
~WebSocket()
|
|
{
|
|
Log($"Free socket with instanceId: {instanceId}");
|
|
WebSocketManager.FreeInstance(instanceId);
|
|
}
|
|
|
|
public void ConnectAsync()
|
|
{
|
|
Log($"Connect with instanceId: {instanceId}");
|
|
WebSocketManager.Add(this);
|
|
int code = WebSocketManager.WebSocketConnect(instanceId);
|
|
if (code < 0) HandleOnError(GetErrorMessageFromCode(code));
|
|
}
|
|
|
|
public void CloseAsync()
|
|
{
|
|
Log($"Close with instanceId: {instanceId}");
|
|
int code = WebSocketManager.WebSocketClose(instanceId, (int)CloseStatusCode.Normal, "Normal Closure");
|
|
if (code < 0) HandleOnError(GetErrorMessageFromCode(code));
|
|
}
|
|
|
|
public void SendAsync(string text)
|
|
{
|
|
Log($"Send, type: {Opcode.Text}, size: {text.Length}");
|
|
int code = WebSocketManager.WebSocketSendStr(instanceId, text);
|
|
if (code < 0) HandleOnError(GetErrorMessageFromCode(code));
|
|
}
|
|
|
|
public void SendAsync(byte[] data)
|
|
{
|
|
Log($"Send, type: {Opcode.Binary}, size: {data.Length}");
|
|
int code = WebSocketManager.WebSocketSend(instanceId, data, data.Length);
|
|
if (code < 0) HandleOnError(GetErrorMessageFromCode(code));
|
|
}
|
|
|
|
internal void HandleOnOpen()
|
|
{
|
|
Log("OnOpen");
|
|
OnOpen?.Invoke(this, new OpenEventArgs());
|
|
}
|
|
|
|
internal void HandleOnMessage(byte[] rawData)
|
|
{
|
|
Log($"OnMessage, type: {Opcode.Binary}, size: {rawData.Length}");
|
|
OnMessage?.Invoke(this, new MessageEventArgs(Opcode.Binary, rawData));
|
|
}
|
|
|
|
internal void HandleOnMessageStr(string data)
|
|
{
|
|
Log($"OnMessage, type: {Opcode.Text}, size: {data.Length}");
|
|
OnMessage?.Invoke(this, new MessageEventArgs(Opcode.Text, data));
|
|
}
|
|
|
|
internal void HandleOnClose(ushort code, string reason)
|
|
{
|
|
Log($"OnClose, code: {code}, reason: {reason}");
|
|
OnClose?.Invoke(this, new CloseEventArgs(code, reason));
|
|
WebSocketManager.Remove(instanceId);
|
|
}
|
|
|
|
internal void HandleOnError(string msg)
|
|
{
|
|
Log("OnError, error: " + msg);
|
|
OnError?.Invoke(this, new ErrorEventArgs(msg));
|
|
}
|
|
|
|
internal static string GetErrorMessageFromCode(int errorCode)
|
|
{
|
|
switch (errorCode)
|
|
{
|
|
case -1: return "WebSocket instance not found.";
|
|
case -2: return "WebSocket is already connected or in connecting state.";
|
|
case -3: return "WebSocket is not connected.";
|
|
case -4: return "WebSocket is already closing.";
|
|
case -5: return "WebSocket is already closed.";
|
|
case -6: return "WebSocket is not in open state.";
|
|
case -7: return "Cannot close WebSocket. An invalid code was specified or reason is too long.";
|
|
default: return $"Unknown error code {errorCode}.";
|
|
}
|
|
}
|
|
|
|
[System.Diagnostics.Conditional("UNITY_WEB_SOCKET_LOG")]
|
|
static void Log(string msg)
|
|
{
|
|
UnityEngine.Debug.Log($"[UnityWebSocket]" +
|
|
$"[{DateTime.Now.TimeOfDay}]" +
|
|
$" {msg}");
|
|
}
|
|
}
|
|
}
|
|
#endif
|