private void onMessage(MessageEventArgs eventArgs) { if (!eventArgs.IsNull()) { OnMessage.Emit(this, eventArgs); } }
//异步统一接口,.... public void FrameMove() { //同步方式... if (OpenHttpAsync) { if (IsSyncHttpDone) { if (_lastMessageEventArgs != null) { OnMessage(this, _lastMessageEventArgs); } } return; } //异步方式... try { var e = dequeueFromMessageEventQueue(); if (e != null) { OnMessage.Emit(this, e); } } catch (Exception ex) { //acceptException (ex, "An exception has occurred while OnMessage."); error(ex.ToString() + " An exception has occurred while OnMessage."); } }
private void startReceiving() { if (_messageEventQueue.Count > 0) { _messageEventQueue.Clear(); } _exitReceiving = new AutoResetEvent(false); _receivePong = new AutoResetEvent(false); Action receive = null; receive = async() => await WebSocketFrame.ReadAsync( _stream, true, async frame => { if (await ProcessWebSocketFrameAsync(frame).ConfigureAwait(false) && _readyState != WebSocketState.Closed) { receive(); if (!frame.IsData) { return; } await _forEvent.WaitAsync().ConfigureAwait(false); try { var e = dequeueFromMessageEventQueue(); if (e != null && _readyState == WebSocketState.Open) { OnMessage.Emit(this, e); } } catch (Exception ex) { await ProcessExceptionAsync(ex, "An exception has occurred while OnMessage.").ConfigureAwait(false); } finally { _forEvent.Release(); } } else if (_exitReceiving != null) { _exitReceiving.Set(); } }, async ex => await ProcessExceptionAsync(ex, "An exception has occurred while receiving a message.")).ConfigureAwait(false); receive(); }
private void startReceiving() { if (_messageEventQueue.Count > 0) { _messageEventQueue.Clear(); } _exitReceiving = new AutoResetEvent(false); _receivePong = new AutoResetEvent(false); Action receive = null; receive = () => WebSocketFrame.ReadAsync( _stream, true, frame => { if (processWebSocketFrame(frame) && _readyState != WebSocketState.Closed) { receive(); if (!frame.IsData) { return; } lock (_forEvent) { try { var e = dequeueFromMessageEventQueue(); if (e != null && _readyState == WebSocketState.Open) { OnMessage.Emit(this, e); } } catch (Exception ex) { processException(ex, "An exception has occurred while OnMessage."); } } } else if (_exitReceiving != null) { _exitReceiving.Set(); } }, ex => processException(ex, "An exception has occurred while receiving a message.")); receive(); }
private void createWebSocket(string uri) { socket = new WebSocket(uri); if (Trace) { socket.Log.Level = LogLevel.Debug; } socket.OnClose += (sender, evt) => { // Release socket handle socket = null; Logger.TraceIf(Trace, String.Format("Socket Closed. Code={0}, Reason={1}", evt.Code, evt.Reason)); OnClose.Emit(this, new WebSocketCloseEventArgs(evt.Code, evt.Reason)); }; socket.OnMessage += (sender, evt) => { if (evt.IsPing) { Logger.TraceIf(Trace, "SocketReceive: WebSocket ping."); return; } if (evt.IsText) { Logger.TraceIf(Trace, "SocketReceive: Invalid content (text/plain)."); return; } OnMessage.Emit(this, new WebSocketMessageEventArgs(evt.RawData)); }; socket.OnError += (sender, evt) => { if (OnError != null) { OnError.Emit(sender, new WebSocketErrorEventArgs(evt.Exception)); } }; socket.OnOpen += (sender, evt) => { if (OnOpen != null) { OnOpen.Emit(sender, evt); } }; }
private void Websocket_OnMessage(object sender, MessageEventArgs e) { var response = e.Data.FromJson <ExpandoObject>(); object message; if (ExpandoHelpers.HasProperty(response, "error")) { message = response.Adapt <WsLoginResponse>(); } else { message = response.Adapt <WsUpdateResponse>(); } OnMessage.Emit(this, new EventWebsocketMessage() { Message = message }); }