static void socketServer_NewSessionConnected(WebSocketSession session) { string name = "null", data = string.Empty; int ifor = 0; int.TryParse(session.Host[0].ToString(), out ifor); msgFor mfor = (msgFor)ifor; switch (mfor) { case msgFor.API: case msgFor.EVENT: data = session.Origin; session.Close(); db.process_MSG(mfor, data); break; default: m_Sessions.Add(session); name = session.Cookies["name"] == null?Guid.NewGuid().ToString().Substring(0, 8) : session.Cookies["name"]; //SendToAll(name + ": " + e); SendToAll("System: " + name + " connected"); break; } }
public static msgFor msgFor_GET(decimal msg_id) { msgFor mfor = msgFor.NONE; int k = 0; int.TryParse(msg_id.ToString()[0].ToString(), out k); mfor = (msgFor)k; return(mfor); }
//public static void Send(wsData m) //{ // Send(m.ToString()); //} public static void Send(decimal msg_id, string sessionid, bool isBase64 = false) { //new Thread(new ThreadStart(() => //{ using (Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { c.BeginConnect(ipEndPoint, new AsyncCallback(ConnectCallback), c); connectDone.WaitOne(); string key = DateTime.Now.ToString("yyMMddHHmmssfff"); string guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; SHA1 sha = new SHA1CryptoServiceProvider(); byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(key + guid)); string _Sec_WebSocket_Key = Convert.ToBase64String(hash); string base64 = msg_id.ToString() + sessionid; //if (isBase64 == false) //{ // //string data = Guid.NewGuid().ToString(); // var bytes = Encoding.UTF8.GetBytes(base64); // base64 = Convert.ToBase64String(bytes); //} msgFor mfor = msg.msgFor_GET(msg_id); //var handshake = "GET / HTTP/1.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: p2z/MFplfpRzjsVywqRQTg==\r\nHost: echo.websocket.org\r\nOrigin: http://echo.websocket.org/\r\n\r\n"; var handshake = "GET / HTTP/1.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: " + _Sec_WebSocket_Key + "\r\nHost: " + ((int)mfor).ToString() + "localhost\r\nOrigin: " + base64 + "\r\n\r\n"; var byteData = Encoding.UTF8.GetBytes(handshake); c.Send(byteData, SocketFlags.Partial); if (c.Connected) { c.Shutdown(SocketShutdown.Both); c.Close(); } } //})).Start(); }