我正在使用Microsoft.Web.WebSockets
如下所示,我如何在连接时分配哪个用户正在连接而不在查询字符串中设置他的 ID,我在服务器中有一个属性项,但在客户端我找不到类似的东西。
这是我的服务器:
using System.Linq;
using Microsoft.Web.WebSockets;
namespace TestSocket
{
public class TestWebSocketHandler : WebSocketHandler
{
private static WebSocketCollection clients = new WebSocketCollection();
private string name;
public override void OnOpen()
{
this.name = this.WebSocketContext.QueryString["name"];
var a = this.WebSocketContext.Items["test"];
var all = clients.Where(n => ((TestWebSocketHandler)n).name == this.name);
if (all.Count() > 0)
{
clients.Remove((TestWebSocketHandler)all.ToList()[0]);
}
clients.Add(this);
}
public override void OnMessage(string message)
{
// WebSocketPacket webso = JsonConvert.DeserializeObject<WebSocketPacket>(message);
// WebSocketPacket response = new WebSocketPacket();
var channel = clients.FirstOrDefault(n => ((TestWebSocketHandler)n).name == message);
if (channel != null)
channel.Send("test 123");
}
public override void OnClose()
{
clients.Remove(this);
}
}
}
这是我的客户:
var ws = new WebSocket("ws://localhost:30455//ws.ashx");
ws.Connect();
ws.Send("BALUS");
以及如何加密服务器和客户端之间发送的数据?