private void ReceivedHanlder(SocketToken token, BufferSegment seg) { if (IsConnected == false) { string msg = _encoding.GetString(seg.buffer, seg.offset, seg.count); _acceptInfo = new WebsocketFrame().ParseAcceptedFrame(msg); if ((IsConnected = _acceptInfo.IsHandShaked())) { _resetEvent.Set(); if (onConnect != null) { onConnect(token, IsConnected); } } else { this._token.DisConnect(); } } else { WebsocketFrame packet = new WebsocketFrame(); bool isOk = packet.DecodingFromBytes(seg, true); if (isOk == false) { return; } if (packet.opCode == 0x01) { if (onReceivedString != null) { onReceivedString(token, _encoding.GetString(packet.payload.buffer, packet.payload.offset, packet.payload.count)); } return; } else if (packet.opCode == 0x08)//close { IsConnected = false; this._token.DisConnect(); } else if (packet.opCode == 0x09)//ping { SendPong(seg); } else if (packet.opCode == 0x0A)//pong { SendPing(); } if (onReceieve != null && packet.payload.count > 0) { onReceieve(token, packet.payload); } } }
public bool Send(string msg, bool waiting = true) { if (IsConnected == false) { return(false); } var buf = new WebsocketFrame().ToSegmentFrame(msg); _token.SendAsync(buf, waiting); return(true); }
public void SendPing() { var buf = new WebsocketFrame().ToSegmentFrame(new byte[] { }, OpCodeType.Bing); _token.SendAsync(buf, true); }
public void SendPong(BufferSegment buf) { var seg = new WebsocketFrame().ToSegmentFrame(buf, OpCodeType.Bong); _token.SendAsync(seg, true); }
private void OnReceieve(SocketToken token, BufferSegment seg) { var connection = ConnectionPool.Find(x => x.sToken.tokenId == token.tokenId); if (connection == null) { connection = new ConnectionInfo() { sToken = token }; ConnectionPool.Add(connection); } if (connection.IsHandShaked == false) { var serverFrame = new WebsocketFrame(); var access = serverFrame.GetHandshakePackage(seg); connection.IsHandShaked = access.IsHandShaked(); if (connection.IsHandShaked == false) { CloseAndRemove(connection); return; } connection.ConnectedTime = DateTime.Now; var rsp = serverFrame.RspAcceptedFrame(access); this.token.SendAsync(token, rsp); connection.accessInfo = access; if (onAccept != null) { onAccept(token); } } else { RefreshTimeout(token); WebsocketFrame packet = new WebsocketFrame(); bool isOk = packet.DecodingFromBytes(seg, true); if (isOk == false) { return; } if (packet.OpCode == 0x01)//text { if (onRecieveString != null) { onRecieveString(token, encoding.GetString(seg.buffer, seg.offset, seg.count)); } return; } else if (packet.OpCode == 0x08)//close { CloseAndRemove(connection); return; } else if (packet.OpCode == 0x09)//ping { SendPong(token, seg); } else if (packet.OpCode == 0x0A)//pong { // SendPing(session.sToken); } if (onReceieve != null && packet.Payload.count > 0) { onReceieve(token, packet.Payload); } } }
private void SendPong(SocketToken token, BufferSegment seg) { var buffer = new WebsocketFrame().ToSegmentFrame(seg, OpCodeType.Bong); this.token.SendAsync(token, buffer); }
public bool Send(SocketToken sToken, string content, bool waiting = true) { var buffer = new WebsocketFrame().ToSegmentFrame(content); return(token.SendAsync(sToken, buffer, waiting)); }