10using System.Collections.Generic;
12using System.Runtime.InteropServices;
125 public static class WebSocketHelpers
160 public static WebSocketException GetErrorMessageFromCode(
int errorCode, Exception inner)
166 case -1:
return new WebSocketUnexpectedException(
"WebSocket instance not found.", inner);
167 case -2:
return new WebSocketInvalidStateException(
"WebSocket is already connected or in connecting state.", inner);
168 case -3:
return new WebSocketInvalidStateException(
"WebSocket is not connected.", inner);
169 case -4:
return new WebSocketInvalidStateException(
"WebSocket is already closing.", inner);
170 case -5:
return new WebSocketInvalidStateException(
"WebSocket is already closed.", inner);
171 case -6:
return new WebSocketInvalidStateException(
"WebSocket is not in open state.", inner);
172 case -7:
return new WebSocketInvalidArgumentException(
"Cannot close WebSocket. An invalid code was specified or reason is too long.", inner);
173 default:
return new WebSocketUnexpectedException(
"Unknown error.", inner);
197 : base(message, inner)
233#if UNITY_WEBGL && !UNITY_EDITOR
237 public class WebSocket: IWebSocket
241 [DllImport(
"__Internal")]
242 public static extern int WebSocketConnect(
int instanceId);
244 [DllImport(
"__Internal")]
245 public static extern int WebSocketClose(
int instanceId,
int code,
string reason);
247 [DllImport(
"__Internal")]
248 public static extern int WebSocketSend(
int instanceId,
byte[] dataPtr,
int dataLength);
250 [DllImport(
"__Internal")]
251 public static extern int WebSocketGetState(
int instanceId);
256 protected int instanceId;
285 this.instanceId = instanceId;
296 WebSocketFactory.HandleInstanceDestroy(this.instanceId);
303 public int GetInstanceId()
306 return this.instanceId;
316 int ret = WebSocketConnect(this.instanceId);
319 throw WebSocketHelpers.GetErrorMessageFromCode(ret,
null);
331 int ret = WebSocketClose(this.instanceId, (
int)code, reason);
334 throw WebSocketHelpers.GetErrorMessageFromCode(ret,
null);
342 public void Send(
byte[] data)
345 int ret = WebSocketSend(this.instanceId, data, data.Length);
348 throw WebSocketHelpers.GetErrorMessageFromCode(ret,
null);
356 public void Send(
string text)
358 var data = Encoding.UTF8.GetBytes(text);
359 int ret = WebSocketSend(this.instanceId, data, data.Length);
362 throw WebSocketHelpers.GetErrorMessageFromCode(ret,
null);
373 int state = WebSocketGetState(this.instanceId);
376 throw WebSocketHelpers.GetErrorMessageFromCode(state,
null);
402 public void DelegateOnOpenEvent()
404 var onOnOpen = this.
OnOpen;
405 if (onOnOpen !=
null) onOnOpen.Invoke();
413 public void DelegateOnMessageEvent(
byte[] data)
416 if (onOnMessage !=
null) onOnMessage.Invoke(data);
424 public void DelegateOnErrorEvent(
string errorMsg)
427 if (onOnError !=
null) onOnError.Invoke(errorMsg);
435 public void DelegateOnCloseEvent(
int closeCode)
438 if (onOnClose !=
null) onOnClose.Invoke(WebSocketHelpers.ParseCloseCodeEnum(closeCode));
469 protected WebSocketSharp.WebSocket
ws;
487 var onOnOpen = this.
OnOpen;
488 if (onOnOpen !=
null) onOnOpen.Invoke();
494 if (ev.RawData !=
null)
497 if (onOnMessage !=
null)
498 onOnMessage.Invoke(ev.RawData);
506 if (onOnError !=
null) onOnError.Invoke(ev.Message);
513 if (onOnClose !=
null)
515 WebSocketHelpers.ParseCloseCodeEnum((
int) ev.Code)
536 if (this.
ws.ReadyState == WebSocketSharp.WebSocketState.Open ||
this.
ws.ReadyState == WebSocketSharp.WebSocketState.Closing)
541 this.
ws.ConnectAsync();
559 if (this.
ws.ReadyState == WebSocketSharp.WebSocketState.Closing)
562 if (this.
ws.ReadyState == WebSocketSharp.WebSocketState.Closed)
567 this.
ws.CloseAsync((ushort)code, reason);
584 if (this.
ws.ReadyState != WebSocketSharp.WebSocketState.Open)
606 if (this.
ws.ReadyState != WebSocketSharp.WebSocketState.Open)
627 if (this.
ws.ReadyState != WebSocketSharp.WebSocketState.Open)
647 switch (this.
ws.ReadyState)
649 case WebSocketSharp.WebSocketState.Connecting:
652 case WebSocketSharp.WebSocketState.Open:
655 case WebSocketSharp.WebSocketState.Closing:
658 case WebSocketSharp.WebSocketState.Closed:
673 public static class WebSocketFactory
676#if UNITY_WEBGL && !UNITY_EDITOR
678 private static Dictionary<Int32, WebSocket> instances =
new Dictionary<Int32, WebSocket>();
681 public delegate
void OnOpenCallback(
int instanceId);
682 public delegate
void OnMessageCallback(
int instanceId, System.IntPtr msgPtr,
int msgSize);
683 public delegate
void OnErrorCallback(
int instanceId, System.IntPtr errorPtr);
684 public delegate
void OnCloseCallback(
int instanceId,
int closeCode);
687 [DllImport(
"__Internal")]
688 public static extern int WebSocketAllocate(
string url);
690 [DllImport(
"__Internal")]
691 public static extern void WebSocketFree(
int instanceId);
693 [DllImport(
"__Internal")]
694 public static extern void WebSocketSetOnOpen(OnOpenCallback callback);
696 [DllImport(
"__Internal")]
697 public static extern void WebSocketSetOnMessage(OnMessageCallback callback);
699 [DllImport(
"__Internal")]
700 public static extern void WebSocketSetOnError(OnErrorCallback callback);
702 [DllImport(
"__Internal")]
703 public static extern void WebSocketSetOnClose(OnCloseCallback callback);
706 private static bool isInitialized =
false;
711 private static void Initialize()
714 WebSocketSetOnOpen(DelegateOnOpenEvent);
715 WebSocketSetOnMessage(DelegateOnMessageEvent);
716 WebSocketSetOnError(DelegateOnErrorEvent);
717 WebSocketSetOnClose(DelegateOnCloseEvent);
719 isInitialized =
true;
728 public static void HandleInstanceDestroy(
int instanceId)
731 instances.Remove(instanceId);
732 WebSocketFree(instanceId);
736 [MonoPInvokeCallback(typeof(OnOpenCallback))]
737 public static void DelegateOnOpenEvent(
int instanceId)
740 WebSocket instanceRef;
742 if (instances.TryGetValue(instanceId, out instanceRef))
744 instanceRef.DelegateOnOpenEvent();
749 [MonoPInvokeCallback(typeof(OnMessageCallback))]
750 public static void DelegateOnMessageEvent(
int instanceId, System.IntPtr msgPtr,
int msgSize)
753 WebSocket instanceRef;
755 if (instances.TryGetValue(instanceId, out instanceRef))
757 byte[] msg =
new byte[msgSize];
758 Marshal.Copy(msgPtr, msg, 0, msgSize);
760 instanceRef.DelegateOnMessageEvent(msg);
765 [MonoPInvokeCallback(typeof(OnErrorCallback))]
766 public static void DelegateOnErrorEvent(
int instanceId, System.IntPtr errorPtr)
769 WebSocket instanceRef;
771 if (instances.TryGetValue(instanceId, out instanceRef))
774 string errorMsg = Marshal.PtrToStringAuto(errorPtr);
775 instanceRef.DelegateOnErrorEvent(errorMsg);
781 [MonoPInvokeCallback(typeof(OnCloseCallback))]
782 public static void DelegateOnCloseEvent(
int instanceId,
int closeCode)
785 WebSocket instanceRef;
787 if (instances.TryGetValue(instanceId, out instanceRef))
789 instanceRef.DelegateOnCloseEvent(closeCode);
800 public static WebSocket CreateInstance(
string url)
802#if UNITY_WEBGL && !UNITY_EDITOR
806 int instanceId = WebSocketAllocate(url);
807 WebSocket wrapper =
new WebSocket(instanceId);
808 instances.Add(instanceId, wrapper);
812 return new WebSocket(url);
Generic WebSocket exception class
WebSocketException(string message)
WebSocketException(string message, Exception inner)
void Send(string data)
Send text data over the socket.
WebSocket(string url)
WebSocket constructor.
WebSocketSharp.WebSocket ws
The WebSocketSharp instance.
void Send(byte[] data)
Send binary data over the socket.
WebSocketMessageEventHandler OnMessage
Occurs when a message is received.
WebSocketErrorEventHandler OnError
Occurs when an error was reported from WebSocket.
WebSocketOpenEventHandler OnOpen
Occurs when the connection is opened.
bool Ping()
Trys to ping the host.
void Connect()
Open WebSocket connection
void Close(WebSocketCloseCode code=WebSocketCloseCode.Normal, string reason=null)
Close WebSocket connection with optional status code and reason.
WebSocketCloseEventHandler OnClose
Occurs when the socked was closed.
WebSocketState GetState()
Return WebSocket connection state.
Invalid argument exception raised when bad arguments are passed to a method.
WebSocketInvalidArgumentException(string message)
WebSocketInvalidArgumentException(string message, Exception inner)
WebSocketInvalidArgumentException()
Invalid state exception raised when trying to invoke action which cannot be done due to different the...
WebSocketInvalidStateException(string message, Exception inner)
WebSocketInvalidStateException(string message)
WebSocketInvalidStateException()
Web socket exception raised when an error was not expected, probably due to corrupted internal state.
WebSocketUnexpectedException(string message)
WebSocketUnexpectedException(string message, Exception inner)
WebSocketUnexpectedException()
WebSocket class interface shared by both native and JSLIB implementation.
WebSocketMessageEventHandler OnMessage
Occurs when a message is received.
WebSocketCloseEventHandler OnClose
Occurs when the socked was closed.
void Send(byte[] data)
Send binary data over the socket.
WebSocketOpenEventHandler OnOpen
Occurs when the connection is opened.
WebSocketErrorEventHandler OnError
Occurs when an error was reported from WebSocket.
void Connect()
Open WebSocket connection
void Close(WebSocketCloseCode code=WebSocketCloseCode.Normal, string reason=null)
Close WebSocket connection with optional status code and reason.
WebSocketState GetState()
Return WebSocket connection state.
delegate void WebSocketCloseEventHandler(WebSocketCloseCode closeCode)
Handler for WebSocket Close event.
WebSocketCloseCode
Web socket close codes.
delegate void WebSocketErrorEventHandler(string errorMsg)
Handler for an error event received from WebSocket.
delegate void WebSocketOpenEventHandler()
Handler for WebSocket Open event.
WebSocketState
Enum representing WebSocket connection state
delegate void WebSocketMessageEventHandler(byte[] data)
Handler for message received from WebSocket.