private void connect() { if (wss != null) { wss.Close(); } wss = new WebSocketSharp.WebSocket("ws://1.85.44.234:9612"); wss.WaitTime = new TimeSpan(0, 0, 8); wss.OnMessage += (s, e1) => { Console.WriteLine(e1.Data); sockobj d = JsonConvert.DeserializeObject <sockobj>(e1.Data); if (d.op == "4D") { update(d); } }; wss.OnOpen += (s, e1) => { String a = "{ctp:\"0\",uid:\"" + user0 + "\",utp:\"1\",op:\"0\"}"; lock (wss_lock) { wss.Send(a); } Console.WriteLine(" websocket open!"); }; wss.OnClose += (s, e1) => { Console.WriteLine("close!"); }; wss.OnError += (s, e1) => { Console.WriteLine("error1"); }; wss.Connect(); }
public bool update(sockobj x) { String stm = ""; int state = 4; String hcd = utr[x.rtu]; String Guid = x.serial; String msg = "未知错误!"; if (x.uid != "service") { return(false); } msg = x.message; if (x.success) { if (x.value == "0101") //开机 { state = 2; lock (_lock) { foreach (x1 o in pending) { if (o.GUID == Guid) { stm = o.STM; end.Add(o); pending.Remove(o); break; } } } } else if (x.value == "0100")//关机 { state = 3; lock (_lock) { foreach (x1 o in pendinge) { if (o.GUID == Guid) { stm = o.STM; ok.Add(o); pendinge.Remove(o); break; } } } } } else { lock (_lock) { foreach (x1 o in pending) { if (o.GUID == Guid) { stm = o.STM; msg = x.message + "[重试(" + o.getretry().ToString() + ")]"; //o.getretry(); //outdate.Add(o); //pendinge.Remove(o); break; } } } } IDictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("action", "updateIrriPlanState"); parameters.Add("state", state.ToString()); parameters.Add("stm", stm); parameters.Add("hcd", hcd); parameters.Add("acttm", DateTime.Now.ToString()); parameters.Add("msg", msg); parameters.Add("id", "1"); parameters.Add("guid", Guid); loginstatus ret = new loginstatus(); HttpWebResponse response = CreatePostHttpResponse(url_update, parameters, null, null, Encoding.UTF8, cookies); if (response != null) { StreamReader sr = new StreamReader(response.GetResponseStream()); String txt = sr.ReadToEnd(); ret = JsonConvert.DeserializeObject <loginstatus>(txt); return(ret.success); } return(true); }