websocket-sharp-customheaders发送标头

时间:2019-11-08 10:02:50

标签: c# websocket http-headers websocket-sharp

我已经为我的WebSocket应用程序安装了nuget软件包websocket-sharp-customheaders,并希望在http-header-information中发送sessionId。 客户代码:

public class SocketController
{
    public static readonly string ConnectionString = "ws://localhost:{0}/{1}";

    private string sessionId;

    public WebSocket NotificationSocket { get; private set; }

    public SocketController(ServiceConfiguration config)
    {
        NotificationSocket = new WebSocket(string.Format(ConnectionString, config.Port, "Notify"));
    }

    public void Connect()
    {
        NotificationSocket.Connect();
    }

    public void SendNotification(string sessionId, Notification data)
    {
        if (NotificationSocket.ReadyState == WebSocketState.Open)
        {
            NotificationSocket.CustomHeaders = new Dictionary<string, string> { { "SessionId", sessionId } };
            NotificationSocket.Send(data.SerializeJson());
        }
    }

    public void SetNotificationSocketOnMessage(Action<NotifyMessage> onMessage)
    {
        NotificationSocket.OnMessage += (sender, e) => onMessage(e.Data.DeserializeJson<NotifyMessage>());
    }

    public void Stop()
    {
        NotificationSocket.Close();
    }
}

在SendNotification中设置头。但是服务器未收到标头信息。

服务器套接字行为:

    public class NotifyService : WebSocketBehavior
    {
        protected override void OnMessage(MessageEventArgs e)
        {
            var command = e.Data;

            if (!Context.Headers.Contains("SessionId"))
                return;

            // Stuff todo with SessionId and Data
        }
    }

有人可以告诉我如何正确发送标头信息吗?

1 个答案:

答案 0 :(得分:0)

https://github.com/sta/websocket-sharp/pull/22

我相信他们不允许这样做。