private void HandleReceive(string content) { if (!string.IsNullOrEmpty(content)) { string type = content.Substring(0, 5); if (type == systemMsg) { //系统消息 string msg = content.Substring(5, 5); if (msg == connectedTag) { //客户端连接成功回复 //客户端的服务器名 string clientServerName = content.Substring(10); //连接到客户端的接收服务 Connect(clientServerName); Debug.WriteLine("客户端已连接,接收服务:" + clientServerName); } } else { //普通消息 string msg = content.Substring(5); //响应事件 OnReceiveMsg?.Invoke(this, msg); Debug.WriteLine("收到消息:" + msg); } } }
private void InternalOnReceiveMsg(int nServiceID, int nConnectID, IntPtr pMsg, int nMsgLen) { var msg = new byte[nMsgLen]; var p = pMsg; for (int i = 0; i < nMsgLen; ++i) { msg[i] = Marshal.ReadByte(p); p += 1; } OnReceiveMsg?.Invoke(nServiceID, nConnectID, msg); }
private void MqttClient_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e) { // Debug.Log(Encoding.UTF8.GetString(e.Message)); if (e.Topic.Equals(this.clientId)) { OnReceiveMsg?.Invoke(this, Encoding.UTF8.GetString(e.Message)); } else { //Debug.Log(e.Topic + " "+Encoding.UTF8.GetString(e.Message)); OnReceiveByteMsg?.Invoke(this, e.Message); } }
private void ReceiveCallBack(IAsyncResult ar) { try { EndPoint ep = ar.AsyncState as IPEndPoint; SocketInfo info = _listSocketInfo[ep.ToString()]; int readCount = 0; if (info.socket == null) { return; } readCount = info.socket.EndReceive(ar); if (readCount > 0) { //byte[] buffer = new byte[readCount]; //Buffer.BlockCopy(info.buffer, 0, buffer, 0, readCount); if (readCount < info.buffer.Length) { byte[] newBuffer = new byte[readCount]; Buffer.BlockCopy(info.buffer, 0, newBuffer, 0, readCount); info.msgBuffer = newBuffer; } else { info.msgBuffer = info.buffer; } string msgTip = Encoding.UTF8.GetString(info.msgBuffer); if (msgTip == "\0\0\0faild") { info.isConnected = false; if (this.OnDisConnected != null) { OnDisConnected(info.socket.RemoteEndPoint.ToString()); } _listSocketInfo.Remove(info.socket.RemoteEndPoint.ToString()); info.socket.Close(); return; } OnReceiveMsg?.Invoke(info.socket.RemoteEndPoint.ToString()); } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } }
public void ReceiveData() { while (IsConnect) { var buffer = new byte[BufferLen]; var len = _clientSocket.Receive(buffer); if (len <= 0) { break; } var msg = this.Encoding.GetString(buffer, 0, len); OnReceiveMsg?.Invoke(msg); } }
/// <summary> /// 添加逻辑处理 /// </summary> /// <param name="id"></param> /// <param name="callback"></param> public static void AddLogicProcess(int id, OnReceiveMsg callback) { List <OnReceiveMsg> list = null; if (!callbackMap.TryGetValue(id, out list)) { list = new List <OnReceiveMsg>(); callbackMap[id] = list; } var t = callback.GetType(); if (list.Find((i) => i.GetType().FullName == t.FullName) == null) { list.Add(callback); } }
/// <summary> /// Runs in background TcpServerThread; Handles incomming TcpClient requests /// </summary> private void ListenForIncommingRequests() { try { // Create listener on localhost port 8052. tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), PORT); tcpListener.Start(); if (debug) { Debug.Log("Server is listening"); } Byte[] bytes = new Byte[1024]; while (true) { using (connectedTcpClient = tcpListener.AcceptTcpClient()) { // Get a stream object for reading using (NetworkStream stream = connectedTcpClient.GetStream()) { int length; // Read incomming stream into byte arrary. while ((length = stream.Read(bytes, 0, bytes.Length)) != 0) { var incommingData = new byte[length]; Array.Copy(bytes, 0, incommingData, 0, length); // Convert byte array to string message. string clientMessage = Encoding.ASCII.GetString(incommingData); if (debug) { Debug.Log("client message received as: " + clientMessage); } OnReceiveMsg.Invoke(clientMessage); } } } } } catch (SocketException socketException) { Debug.Log("SocketException " + socketException.ToString()); } }
/// <summary> /// Called when a message is received /// </summary> /// <param name="message">string msg</param> public virtual void MessageReceived(string message) { _message = message; OnReceiveMsg?.Invoke(message); }
public void showMessage(string msg) { OnReceiveMsg?.Invoke(msg, new EventArgs()); }
public void SetOnReceiveListener(OnReceiveMsg receive) { this.receive = receive; }
public void RaiseOnReceiveMsg(string msg, string sessionID) { OnReceiveMsg?.Invoke(msg, sessionID); }