protected override void NewConnection(object sender, SocketArgs args) { try { string request = string.Format( "GET {0} HTTP/1.1" + Environment.NewLine + "Upgrade: WebSocket" + Environment.NewLine + "Connection: Upgrade" + Environment.NewLine + "Host: {1}" + Environment.NewLine + "Origin: {2}" + Environment.NewLine + "Pragma: no-cache" + Environment.NewLine + "Cache-Control: no-cache" + Environment.NewLine + "Accept-Encoding: gzip, deflate" + Environment.NewLine + "Sec-WebSocket-Version: 13" + Environment.NewLine + "Sec-WebSocket-Key: " + Hybi.GenerateKey() + "" + Environment.NewLine + "Sec-WebSocket-Extensions: permessage-deflate; client_no_context_takeover; server_no_context_takeover" + Environment.NewLine + Environment.NewLine, "/", args.Socket.Endpoint, "null"); if (!this.SendRaw(Utils.ToLatin(request))) { Console.WriteLine("Sendraw failed?"); throw new Exception("Sendraw failed?"); } } catch (ObjectDisposedException ex) { Console.WriteLine(ex.Message); return; } }
private void SendBytes(byte[] Buffer) { int MinCompress = 250; bool bCompress = (Encoder != null); if (bCompress && (Buffer.Length <= MinCompress)) { bCompress = false; } #if DEBUG bCompress = (Encoder != null); #endif if (bCompress) { Buffer = Encoder.Compress(Buffer); } byte[] bHybi = Hybi.HybiEncode(Buffer, bCompress); if (!SendRaw(bHybi)) { Console.WriteLine("Sendraw failed?"); throw new Exception("Sendraw failed?"); } }
//public bool Append(Peer Peer, BufferData Buffer) //{ // return Append(Peer.xCon, Buffer); //} public bool Append(WebsocketHandler wHandler, RawSocket SourceSocket, BufferData Buffer) { if (wHandler == null) { return(false); } if (!handshakeComplete) { if (wHandler.Socket == null) { wHandler.Socket = SourceSocket; } string sNew = Utils.FromLatin(Buffer.Data, 0, Buffer.Data.Length); dataString.Append(sNew); if (sNew.EndsWith("\r\n")) { string sData = dataString.ToString(); if (sData.EndsWith("\r\n\r\n")) { if (Header != null) { Header(this, new TextArgs(sData)); } dataString = new StringBuilder(); } } return(true); } if (dataString.Length == 0) { ushort dummy = 0; dataLength = Hybi.HybiLength(Buffer.Data, ref dummy); if (dataLength == 0) { return(false); } } ushort left = 0; byte[] newData = null; int end = Math.Min(Buffer.Data.Length, dataLength - dataString.Length); dataString.Append(Utils.FromLatin(Buffer.Data, 0, end)); if (dataString.Length != dataLength) { return(true); } if (end < Buffer.Data.Length) { left = (ushort)(Buffer.Data.Length - end); } byte[] bData = Utils.ToLatin(dataString.ToString()); string sRet = Hybi.HybiDecode(bData, ref Mask); if (sRet.Length == 0) { return(false); } dataString = new StringBuilder(); if (left > 0) { newData = new byte[left]; Array.Copy(Buffer.Data, end, newData, 0, left); } if ((sRet.Substring(0, 1) != "{") || (sRet.Substring(sRet.Length - 1) != "}")) { Utils.Error("Invalid JSON: " + sRet); return(false); } if (Command != null) { cMsg Cmd = null; try { Cmd = new cMsg(sRet); } catch (Exception ex) { Utils.Error("Invalid JSON: " + ex.Message); return(false); } if (Command != null) { Command(wHandler, Cmd); } } if (newData != null) { return(Append(wHandler, SourceSocket, new BufferData(newData, newData.Length))); } return(true); }