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)); } }
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 ConnectAsync() { Log($"Connect with instanceId: {instanceId}"); WebSocketManager.Add(this); int code = WebSocketManager.WebSocketConnect(instanceId); if (code < 0) { HandleOnError(GetErrorMessageFromCode(code)); } }
public static void CreateInstance() { GameObject go = GameObject.Find("/" + rootName); if (!go) { go = new GameObject(rootName); } _instance = go.GetComponent <WebSocketManager>(); if (!_instance) { _instance = go.AddComponent <WebSocketManager>(); } }
internal void AllocateInstance() { instanceId = WebSocketManager.AllocateInstance(this.Address); Log($"Allocate socket with instanceId: {instanceId}"); if (this.SubProtocols == null) { return; } foreach (var protocol in this.SubProtocols) { if (string.IsNullOrEmpty(protocol)) { continue; } Log($"Add Sub Protocol {protocol}, with instanceId: {instanceId}"); int code = WebSocketManager.WebSocketAddSubProtocol(instanceId, protocol); if (code < 0) { HandleOnError(GetErrorMessageFromCode(code)); break; } } }
internal void HandleOnClose(ushort code, string reason) { Log($"OnClose, code: {code}, reason: {reason}"); OnClose?.Invoke(this, new CloseEventArgs(code, reason)); WebSocketManager.Remove(instanceId); }
void Awake() { DontDestroyOnLoad(gameObject); _instance = this; }