public IHttpActionResult UserLoginScan(ScanLoginModel model) { ApiServerMsg result = new ApiServerMsg(); try { //new空对象 MySocket socket = new MySocket(); XzyWeChatThread xzy = new XzyWeChatThread(socket, model.devicename); DicSocket dicSocket = new DicSocket() { socket = socket, weChatThread = xzy, dateTime = DateTime.Now }; XzyWebSocket._dicSockets.Remove(model.uuid); XzyWebSocket._dicSockets.Add(model.uuid, dicSocket); while (xzy.ScanQrCode == "") { Thread.Sleep(200); } result.Success = true; result.Context = xzy.ScanQrCode; return(Ok(result)); } catch (Exception ex) { result.Success = false; result.ErrContext = ex.Message; return(Ok(result)); } }
public IHttpActionResult UserLogin62(UserLoginModel model) { ApiServerMsg result = new ApiServerMsg(); try { //new空对象 MySocket socket = new MySocket(); if (XzyWebSocket._dicSockets.ContainsKey(model.uuid) && !model.isreset) { XzyWebSocket._dicSockets[model.uuid].socket = socket; //更新微信线程socket,回调消息 XzyWebSocket._dicSockets[model.uuid].weChatThread._socket = socket; } else//否则创建连接池 { XzyWeChatThread xzy = new XzyWeChatThread(socket, model.username, model.password, model.str62, model.devicename); DicSocket dicSocket = new DicSocket() { socket = socket, weChatThread = xzy, dateTime = DateTime.Now }; XzyWebSocket._dicSockets.Remove(model.uuid); XzyWebSocket._dicSockets.Add(model.uuid, dicSocket); } result.Success = true; result.Context = "调用成功,如未登陆成功可能账号受限,请使用websocket登陆查看详细原因"; return(Ok(result)); } catch (Exception e) { result.Success = false; result.ErrContext = e.Message; return(Ok(result)); } }
/// <summary> /// 初始化socket服务 /// </summary> public static void Init() { new WebSocketServer("ws://0.0.0.0:" + ConfigurationManager.AppSettings["WebSocketHost"]).Start(delegate(IWebSocketConnection socket) { socket.OnOpen = delegate { string baseUrl = ""; MyUtils.ParseUrl(socket.ConnectionInfo.Path, out baseUrl, out NameValueCollection nvc); string a = nvc["action"]; string key = nvc["uuid"]; string devicename = nvc["devicename"]; string a2 = nvc["isreset"]; string proxy = nvc["proxy"]; string proxyname = nvc["proxyname"]; string proxypwd = nvc["proxypwd"]; string text = nvc["proxytype"]; if (text == "") { text = "1"; } ScanLoginModel model = new ScanLoginModel { devicename = devicename, proxy = proxy, proxyname = proxyname, proxypwd = proxypwd, proxytype = text.ConvertToInt32() }; if (a == "scan") { if (_dicSockets.ContainsKey(key) && a2 == "false") { _dicSockets[key].socket = socket; _dicSockets[key].weChatThread._socket = socket; _dicSockets[key].weChatThread.SocketIsConnect = true; } else { XzyWeChatThread xzyWeChatThread = new XzyWeChatThread(socket, model); DicSocket value = new DicSocket { socket = socket, weChatThread = xzyWeChatThread }; _dicSockets.Remove(key); _dicSockets.Add(key, value); xzyWeChatThread.SocketIsConnect = true; } } else if (a == "62") { string username = nvc["username"]; string password = nvc["password"]; string str = nvc["str62"]; string proxy2 = nvc["proxy"]; string proxyname2 = nvc["proxyname"]; string proxypwd2 = nvc["proxypwd"]; string s = nvc["proxytype"]; UserLoginModel model2 = new UserLoginModel { username = username, password = password, str62 = str, proxy = proxy2, proxyname = proxyname2, proxypwd = proxypwd2, proxytype = s.ConvertToInt32() }; if (_dicSockets.ContainsKey(key) && a2 == "false") { _dicSockets[key].socket = socket; _dicSockets[key].weChatThread._socket = socket; _dicSockets[key].weChatThread.SocketIsConnect = true; } else { XzyWeChatThread xzyWeChatThread2 = new XzyWeChatThread(socket, model2); DicSocket value2 = new DicSocket { socket = socket, weChatThread = xzyWeChatThread2 }; _dicSockets.Remove(key); _dicSockets.Add(key, value2); xzyWeChatThread2.SocketIsConnect = true; } } }; socket.OnClose = delegate { try { (from p in _dicSockets where p.Value.socket == socket select p).ToList().FirstOrDefault().Value.weChatThread.SocketIsConnect = false; } catch (Exception) { } }; socket.OnMessage = delegate { }; }); }
/// <summary> /// 初始化socket服务 /// </summary> public static void Init() { var server = new WebSocketServer($"ws://0.0.0.0:{ConfigurationManager.AppSettings["WebSocketHost"]}"); server.Start(socket => { socket.OnOpen = () => { string url = ""; NameValueCollection queryString; MyUtils.ParseUrl(socket.ConnectionInfo.Path, out url, out queryString); string action = queryString["action"]; string uuid = queryString["uuid"]; string devicename = queryString["devicename"]; string isreset = queryString["isreset"]; if (action == "scan") { //如果连接池包含 则更新socket if (_dicSockets.ContainsKey(uuid) && isreset == "false") { _dicSockets[uuid].socket = socket; //更新微信线程socket,回调消息 _dicSockets[uuid].weChatThread._socket = socket; _dicSockets[uuid].weChatThread.SocketIsConnect = true; } else//否则创建连接池 { XzyWeChatThread xzy = new XzyWeChatThread(socket, devicename); DicSocket dicSocket = new DicSocket() { socket = socket, weChatThread = xzy }; _dicSockets.Remove(uuid); _dicSockets.Add(uuid, dicSocket); xzy.SocketIsConnect = true; } } else if (action == "62") { string username = queryString["username"]; string password = queryString["password"]; string str62 = queryString["str62"]; //如果连接池包含 则更新socket if (_dicSockets.ContainsKey(uuid) && isreset == "false") { _dicSockets[uuid].socket = socket; //更新微信线程socket,回调消息 _dicSockets[uuid].weChatThread._socket = socket; _dicSockets[uuid].weChatThread.SocketIsConnect = true; } else//否则创建连接池 { XzyWeChatThread xzy = new XzyWeChatThread(socket, username, password, str62, devicename); DicSocket dicSocket = new DicSocket() { socket = socket, weChatThread = xzy }; _dicSockets.Remove(uuid); _dicSockets.Add(uuid, dicSocket); xzy.SocketIsConnect = true; } } }; socket.OnClose = () => { try { _dicSockets.Where(p => p.Value.socket == socket).ToList().FirstOrDefault().Value.weChatThread.SocketIsConnect = false; } catch (Exception ex) { } Console.WriteLine("连接断开!"); }; socket.OnMessage = message => { //Console.WriteLine(message); //allSockets.ToList().ForEach(s => s.Send("Echo: " + message)); }; }); }
/// <summary> /// websocket监听 /// </summary> /// <param name="context"></param> /// <returns></returns> private async Task ProcessRequest(AspNetWebSocketContext context) { var socket = context.WebSocket; string uuid = context.QueryString["uuid"].ToString(); XzyWeChatThread xzy = null; DicSocket dicSocket = new DicSocket() { socket = socket, weChatThread = xzy }; if (_dicSockets.ContainsKey(uuid)) { try { await _dicSockets[uuid].socket.CloseAsync(WebSocketCloseStatus.Empty, string.Empty, CancellationToken.None);//如果client发起close请求,对client进行ack } catch (Exception ex) { LogServer.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "socketErr:" + ex.Message); } } _dicSockets.Add(uuid, dicSocket); while (true) { var buffer = new ArraySegment <byte>(new byte[1024]); var receivedResult = await socket.ReceiveAsync(buffer, CancellationToken.None);//对web socket进行异步接收数据 if (receivedResult.MessageType == WebSocketMessageType.Close) { try { //从连接池移除对象,如果想保持websocket断线后 微信继续执行可以不做此操作 #region 微信注销线程释放 //string myuuid = _dicSockets.Where(p => p.Value.socket.Equals(socket)).FirstOrDefault().Key; //_dicSockets[myuuid].weChatThread.Wx_Logout(); //_dicSockets[myuuid].weChatThread = null; //_dicSockets.Remove(myuuid); #endregion 微信注销线程释放 await socket.CloseAsync(WebSocketCloseStatus.Empty, string.Empty, CancellationToken.None);//如果client发起close请求,对client进行ack } catch (Exception ex) { LogServer.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "socketErr:" + ex.Message); } break; } if (socket.State == System.Net.WebSockets.WebSocketState.Open) { string recvMsg = Encoding.UTF8.GetString(buffer.Array, 0, receivedResult.Count); SocketModel model = JsonConvert.DeserializeObject <SocketModel>(recvMsg); switch (model.action.ToLower()) { case "start": //创建socket await Task.Factory.StartNew(() => { xzy = new XzyWeChatThread(socket); }); break; case "start62": //通过账号密码+62数据登录 await Task.Factory.StartNew(() => { SocketStart62 socketStart62 = JsonConvert.DeserializeObject <SocketStart62>(model.context); xzy = new XzyWeChatThread(socket, socketStart62.username, socketStart62.password, socketStart62.str62); }); break; } } } }