private bool tcpServer_SessionReceived(TcpSession session, byte[] data) { if (!session.IsHandshaked) { // from tablet: /* * GET /Typhoon HTTP/1.1 * Upgrade: WebSocket * Connection: Upgrade * Host: 192.168.0.102:2013 * Origin: http://192.168.0.102:81 * Sec-Websocket-Key: +d6dlnAp7rrQ3otS7Zvi7g== * Sec-WebSocket-Version: 13 +d6dlnAp7rrQ3otS7Zvi7g==: websocket +d6dlnAp7rrQ3otS7Zvi7g==: Upgrade */ // from local: /* * GET /Typhoon HTTP/1.1 * Upgrade: WebSocket * Connection: Upgrade * Host: localhost:2013 * Origin: http://localhost:81 * Sec-Websocket-Key: K92AZtSFpS+9OgiwcPMheg== * Sec-WebSocket-Version: 13 * K92AZtSFpS+9OgiwcPMheg==: websocket * K92AZtSFpS+9OgiwcPMheg==: x-webkit-deflate-frame * K92AZtSFpS+9OgiwcPMheg==: Upgrade */ // location: ws://localhost:2013 // origin: "http://localhost:81" WSClientHandshake chs = WSClientHandshake.FromBytes(data); //if (chs.IsValid && "ws://" + chs.Host == location && chs.Origin == origin) //if (chs.IsValid && "ws://" + chs.Host == location) if (chs.IsValid) { WSServerHandshake shs = new WSServerHandshake(chs.Key); string stringShake = shs.ToString(); byte[] byteResponse = Encoding.UTF8.GetBytes(stringShake); session.Send(byteResponse); session.IsHandshaked = true; // for debug: //client.Send(WebSocketDataFrame.WrapString("Hello from server!!!")); return(false); } else { return(true); // disconnect client } } else { WSDataFrame frame = new WSDataFrame(data); byte[] payload = null; if (frame.IsValid() && frame.FIN) { payload = frame.GetPayload(); } // for debug: //string s = new string(Encoding.UTF8.GetChars(payload)); //string b = ""; //b += s; return(SessionDataReceived != null?SessionDataReceived(session, payload) : false); } }
private bool tcpServer_SessionReceived(TcpSession session, byte[] data) { if (!session.IsHandshaked) { // from tablet: /* GET /Typhoon HTTP/1.1 Upgrade: WebSocket Connection: Upgrade Host: 192.168.0.102:2013 Origin: http://192.168.0.102:81 Sec-Websocket-Key: +d6dlnAp7rrQ3otS7Zvi7g== Sec-WebSocket-Version: 13 +d6dlnAp7rrQ3otS7Zvi7g==: websocket +d6dlnAp7rrQ3otS7Zvi7g==: Upgrade */ // from local: /* GET /Typhoon HTTP/1.1 Upgrade: WebSocket Connection: Upgrade Host: localhost:2013 Origin: http://localhost:81 Sec-Websocket-Key: K92AZtSFpS+9OgiwcPMheg== Sec-WebSocket-Version: 13 K92AZtSFpS+9OgiwcPMheg==: websocket K92AZtSFpS+9OgiwcPMheg==: x-webkit-deflate-frame K92AZtSFpS+9OgiwcPMheg==: Upgrade */ // location: ws://localhost:2013 // origin: "http://localhost:81" WSClientHandshake chs = WSClientHandshake.FromBytes(data); //if (chs.IsValid && "ws://" + chs.Host == location && chs.Origin == origin) //if (chs.IsValid && "ws://" + chs.Host == location) if (chs.IsValid) { WSServerHandshake shs = new WSServerHandshake(chs.Key); string stringShake = shs.ToString(); byte[] byteResponse = Encoding.UTF8.GetBytes(stringShake); session.Send(byteResponse); session.IsHandshaked = true; // for debug: //client.Send(WebSocketDataFrame.WrapString("Hello from server!!!")); return false; } else return true; // disconnect client } else { WSDataFrame frame = new WSDataFrame(data); byte[] payload = null; if (frame.IsValid() && frame.FIN) payload = frame.GetPayload(); // for debug: //string s = new string(Encoding.UTF8.GetChars(payload)); //string b = ""; //b += s; return SessionDataReceived != null ? SessionDataReceived(session, payload) : false; } }