【问题标题】:websocket-sharp-customheaders send headerswebsocket-sharp-customheaders 发送标头
【发布时间】:2019-11-08 10:02:50
【问题描述】:

我已经为我的 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
        }
    }

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2016-06-17
      相关资源
      最近更新 更多