Is there a way to format the HostName for a StreamSocket to include a user token? I get a "Parameter is Incorrect" exceptions when I try to use a URI with the a user Token. I have had success with a MessageWebSocket but I need a StreamSocket because it is able to (easily) run in the background on Windows 10 UWP. Here is my code. Any help would be greatly appreciated.
public static async void ConnectToStreamSocket()
{
StreamSocket socket = new StreamSocket();
string hostUri = "//example.myexample.com/websocket/dfkjlkdfskldklfdkddkfdkffjf";
HostName host = new HostName(hostUri);
string proto = "wss";
await socket.ConnectAsync(host, proto);
}
The parameter of HostName constructor is not a URI, but a string that contains a
hostnameor an IP address. According to the description of thehostNameparameter, it can contain one of the following:For
"//example.myexample.com/websocket/dfkjlkdfskldklfdkddkfdkffjf", this is a URI that don't meet above requirements so that the it cannot be recognized byHostNameclass. Value likeexample.myexample.comcan be recognized.For why you can successfully use
MessageWebSocketclass, this is becauseMessageWebSocket.ConnectAsyncmethod requires a URI parameter as you provided.WebSocketsprovide a mechanism for fast, secure two-way communication between a client and a server over the web using HTTP(S).Socketcan be used for communicating with other device. They are not the same things, we cannot simply transferWebSocketstoStreamSocket. More details aboutWebSocketsandStreamSocketsin uwp please reference the official documentsSocketsandWebSockets.But
WebSocketscan be used in background, you may not needStreamSocket. If you are using WebSockets in background, you must use ControlChannelTrigger. Details for how to use WebSockets with ControlChannelTrigger please reference ControlChannelTrigger with WebSockets.