public void Connect(string url, string path = "/socket.io", string nsp = "/") { if (_socket != null) { _socket.disconnect(); } Url = url; Nsp = nsp; var options = new EngineIO.Options(); _socket = new SocketIO(new WebSocketSharpImpl(), url, path, nsp, options); _socket .on("connect", data => { _isConnected = true; if (EventConnectionChange != null) { EventConnectionChange(this, new EventArgs <bool>(true)); } }) .on("disconnect", data => { this._isConnected = false; if (EventConnectionChange != null) { EventConnectionChange(this, new EventArgs <bool>(false)); } if (_requestDisconnect) { _requestDisconnect = false; if (EventDisconnect != null) { EventDisconnect(this, new EventArgs()); } } }); _socket.EventReceive += (s, e) => { if (EventReceive != null) { EventReceive(this, new EventArgs <EventData>(new EventData(e.Value.Name, e.Value.Data))); } if (_requestQueue.Contains(e.Value.Name)) { _requestQueue.Receive(e.Value.Name, e.Value.Data); } }; if (EventNewSocketInstance != null) { EventNewSocketInstance(this, new EventArgs()); } }
public SocketIO(IWebsocket websocket, string url, string path = "/socket.io", string nsp = "/", EngineIO.Options options = null) { if (!nsp.StartsWith("/")) { throw new Exception("nsp must begin with '/'"); } this.nsp = nsp; _engineio = new EngineIO(websocket, url, path, options); _engineio.debug = false; _engineio .on("ping", data => { this.emit("ping"); }) .on("pong", data => { this.emit("pong"); }) .on("message", (data) => { if (data is string) { var packet = new SocketIOPacket(data as string); Logger.Log(string.Format("receive package type = '{0}', data = {1}", packet.Type, data)); switch (packet.Type) { case SocketIOPacketType.connect: if (nsp != packet.nsp) { _nspConnectionRequest = true; _engineio.send(nsp.CreateNspConnection().Serialize()); } else { _id = packet.Data.ExtractSid(); connected = true; if (_isReconnecting) { _isReconnecting = false; emit("reconnect", _attemptNumber); emit("connect"); } else { emit("connect"); } } break; case SocketIOPacketType.eventMessage: var e = packet.ExtractEvent(); emit(e.Name, e.Data); if (EventReceive != null) { EventReceive(this, new EventArgs <SocketIOEvent>(new SocketIOEvent(e.Name, e.Data))); } break; case SocketIOPacketType.disconnect: disconnect(); break; case SocketIOPacketType.error: if (_nspConnectionRequest) { _nspConnectionRequest = false; if (_isReconnecting) { emit("reconnect_error", packet.Data); } else { emit("connect_error", packet.Data); } _engineio.disconnect(); } else { emit("error", packet.Data); } break; } } }) .on("close", (data) => { connected = false; if (_disconnectRequest) { _disconnectRequest = false; emit("disconnect"); } else { if (!_isReconnecting) { emit("connect_error", data); } if (_attemptNumber >= _engineio.options.reconnectionAttempts) { emit("reconnect_failed"); emit("disconnect"); } else if (_engineio.options.autoConnect) { _isReconnecting = true; if (_attemptNumber > 0) { emit("reconnect_error", data); } _attemptNumber += 1; emit("reconnecting", _attemptNumber); _reconnectTimer.Interval = _engineio.options.reconnectionDelay; _reconnectTimer.Start(); } } }) .on("error", data => { emit("error", data); }); _reconnectTimer = new Timer(); _reconnectTimer.AutoReset = false; _reconnectTimer.Elapsed += (s, e) => { _engineio.connect(); }; }
public void Connect(string url, string nsp = "/") { if (_socket != null) { _socket.disconnect(); } Url = url; Nsp = nsp; var options = new EngineIO.Options(); _socket = new SocketIO(new WebSocketSharpImpl(), url, nsp, options); _socket .on("connect", data => { _isConnected = true; if (EventConnectionChange != null) { EventConnectionChange(this, new EventArgs <bool>(true)); } }) .on("disconnect", data => { this._isConnected = false; if (EventConnectionChange != null) { EventConnectionChange(this, new EventArgs <bool>(false)); } if (_requestDisconnect) { _requestDisconnect = false; if (EventDisconnect != null) { EventDisconnect(this, new EventArgs()); } } }) .on("reconnecting", data => { if (EventReconnecting != null) { EventReconnecting(this, new EventArgs <int>((int)data)); } }) .on("reconnect", data => { this._isConnected = false; if (EventReconnected != null) { EventReconnected(this, new EventArgs <int>((int)data)); } }) .on("exception", data => { var error = (data as JToken).ToObject <NestJSWSException>(); if (EventNestJSException != null) { EventNestJSException(this, new EventArgs <NestJSWSException>(error)); } }); _socket.EventReceive += (s, e) => { if (EventReceive != null) { EventReceive(this, new EventArgs <EventData>(new EventData(e.Value.Name, e.Value.Data))); } if (_requestQueue.Contains(e.Value.Name)) { _requestQueue.Receive(e.Value.Name, e.Value.Data); } }; if (EventNewSocketInstance != null) { EventNewSocketInstance(this, new EventArgs()); } }