简体   繁体   English

如何将邮件从服务器发送到特定客户端

[英]How to send messages from server to a specific client

I am using WebSockets and trying to set up a code base for my server-client. 我正在使用WebSockets,并尝试为我的服务器-客户端设置代码库。 I know how to send messages from client to server and I also know how to listen those messages from the server side. 我知道如何将消息从客户端发送到服务器,也知道如何从服务器端监听这些消息。

However, how can I send a message back to a client ? 但是,如何将消息发送回客户端?

// here is the _clientSocket that I accepted
_clientSocket = _serverSocket.Accept();
int received = _clientSocket.Receive(_buffer, 0, _buffer.Length,
    SocketFlags.None);
// here is the message I got from the client
string receivedMsg = Encoding.ASCII.GetString(_buffer);

if (receivedMsg == "1") 
//TO DO: send back to client "This is a test message from server".

Basically you have to use the SendAsync method: 基本上,您必须使用SendAsync方法:

var sendbuffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes("Whatever text you want to send"));

await socket.SendAsync(sendbuffer , WebSocketMessageType.Text, true, CancellationToken.None)
            .ConfigureAwait(false);

Take a look at this example: https://stackoverflow.com/a/26274839/307976 看一下这个例子: https : //stackoverflow.com/a/26274839/307976

您必须致电:

_clientsocket.Send(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用rabbitMq从服务器发送客户端特定的答案 - How to send an client specific answer from an server using rabbitMq 将数据从服务器发送到特定客户端 - Send data from server to a specific client 将 Socket 字符串从服务器发送到特定客户端 - Send Socket string from server to specific client 从TCP服务器向客户端发送多条消息(C sharp to Android) - send multiple messages from TCP server to Client(C sharp to Android) 如何使用单个 TcpClient 实例将多个字符串消息从客户端发送到服务器? - How can I send multiple string messages from client to server using a single instance of TcpClient? 如何使用 SignalR 集线器将消息从服​​务器发送到客户端 - How do I send messages from server to client using SignalR Hubs 如何在不重置TCP客户端的情况下使用按钮从客户端发送消息? - How to Send Messages From Client With Button Without Resetting TCP Client? 使用TCP c#将数据包从服务器发送到特定客户端 - Send packet from server to a specific client using TCP c# 如何从服务器读取数据并将其发送到客户端? - How to read and send data to client from server? 如何将对象从客户端发送到服务器并反向发送? - How to send an object from client to server and reverse?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM