-1

i am using Microsoft.Web.WebSockets as shown below, how can i assign on connect which user is connecting without setting his id in query string, i have in the server a property items, but in the client i can't find anything like that.

this is my server:

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);

        }
    }
}

this is my client:

var ws = new WebSocket("ws://localhost:30455//ws.ashx");
ws.Connect();
ws.Send("BALUS");

and how can i encrypt the data sent between server and client?

User7291
  • 1,095
  • 3
  • 29
  • 71

1 Answers1

0

how can i assign on connect which user is connecting without setting his id in query string

The WebSocket connection handshake is performed using an HTTP GET request. In a GET request, there are only two ways to pass custom data to the HTTP server - via the URL query string, and via a custom HTTP header. Most WebSocket client libraries do not allow you to specify custom HTTP headers during a WebSocket handshake, so that leaves just the URL query string (if you were writing your own HTTP client, then you could send whatever HTTP headers you wanted).

how can i encrypt the data sent between server and client?

Use wss:// instead of ws://

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770