/// <summary> /// 接收 Client Socket 数据 /// 私有函数不对外直接提供。 /// 使用Receive(Socket socket, List<CommunicateVO> cos)接口监听数据 /// </summary> /// <param name="ar"></param> private void ReceiveCallback(IAsyncResult ar) { String content = String.Empty; StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead)); content = state.sb.ToString(); // 是否有结束标签 if (content.IndexOf("####") > -1) { List <CommunicateVO> cos = CommunicateUtils.strToCommunicateVos(content); Receive(handler, cos); // 重置数据传输对象 state.cleanData(); } } // 继续获取数据 handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); }
public void initServer(string ip, int port) { var server = new WebSocketServer(string.Format("ws://{0}:{1}", ip, port)); server.RestartAfterListenError = true; server.Start(socket => { socket.OnOpen = () => { if (!clientMap.ContainsKey(socket.ConnectionInfo.ClientPort)) { clientMap.Add(socket.ConnectionInfo.ClientPort, socket); } }; socket.OnClose = () => { if (clientMap.ContainsKey(socket.ConnectionInfo.ClientPort)) { clientMap.Remove(socket.ConnectionInfo.ClientPort); } }; socket.OnMessage = message => { Console.WriteLine("OnMessage", socket.ConnectionInfo); List <CommunicateVO> cos = CommunicateUtils.strToCommunicateVos(message); Receive(socket, cos); }; }); }
private void dealCallExe(IWebSocketConnection socket, CallExeCo co) { // exe 调用服务 CallExeServer exeServer = new CallExeServer(); // 响应数据对象 CallExeCro exeCro = new CallExeCro(); exeCro.Uuid = co.Uuid; if (co.Async) { logReqMsg(socket, "exePath:" + co.ExePath + ", Args:" + string.Join(",", co.Args), co.Uuid); // CallExeOutputHandler outputHandler = new CallExeOutputHandler(); // async exeServer.asyncCallExe(co.ExePath, co.Args, output => { Console.WriteLine("output:" + output); // 每次只输出一行,因此需要动态添加"\n" exeCro.Output += output + "\n"; }, exitCode => { Console.WriteLine("exitCode:" + exitCode); exeCro.ExitCode = exitCode; if (exeCro.Output != null && exeCro.Output.Length >= 1) { // 去除最后一个"\n" exeCro.Output = exeCro.Output.Substring(0, exeCro.Output.Length - 1); } // logResMsg(socket, "exitCode:"+ exeCro.ExitCode + ", output:"+ exeCro.Output, co.Uuid); // 异步调用结束 socket.Send(CommunicateUtils.communicateVoToStr(exeCro)); }, error => { Console.WriteLine("error:" + error); exeCro.Error += error; }); } else { // sync string[] ret = exeServer.syncCallExe(co.ExePath, co.Args); Console.WriteLine("exitCode:{0}, ouput:{1}, error:{2}", ret[0], ret[1], ret[2]); exeCro.ExitCode = int.Parse(ret[0]); exeCro.Output = ret[1]; exeCro.Error = ret[2]; socket.Send(CommunicateUtils.communicateVoToStr(exeCro)); } }
private void ReceiveCallback(IAsyncResult ar) { try { // Retrieve the state object and the client socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket client = state.workSocket; // Read data from the remote device. int bytesRead = client.EndReceive(ar); if (bytesRead > 0) { state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead)); string content = state.sb.ToString(); if (content.IndexOf("####") > -1) { List <CommunicateVO> cos = CommunicateUtils.strToCommunicateVos(content); Receive(client, cos); } else { // Get the rest of the data. client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } } else { //string content = state.sb.ToString(); //if (content.IndexOf("####") >= 0) //{ // List<CommunicateVO> cos = CommunicateUtils.strToCommunicateVos(content); // Receive(client, cos); //} // Signal that all bytes have been received. receiveDone.Set(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
private void dealDownloadFile(IWebSocketConnection socket, DownloadFileCo co) { DownloadFileServer server = new DownloadFileServer(); logReqMsg(socket, "URL:" + co.Url + ", savePath:" + co.SavePath, co.Uuid); server.downloadFile(co.Url, co.SavePath, delegate(DownloadFileCro cro) { cro.Uuid = co.Uuid; logResMsg("isSuccess:" + cro.IsSuccess + ", savePath:" + cro.LocalPath + ", Message:" + cro.Message, co.Uuid); // 异步调用结束 socket.Send(CommunicateUtils.communicateVoToStr(cro)); server = null; }); }
/// <summary> /// 向Client Socket 发送数据 /// </summary> /// <param name="socket"></param> /// <param name="co"></param> public void Send(Socket socket, CommunicateVO co) { string data = CommunicateUtils.communicateVoToStr(co); Send(socket, data); }