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
hostname
or an IP address. According to the description of thehostName
parameter, 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 byHostName
class. Value likeexample.myexample.com
can be recognized.For why you can successfully use
MessageWebSocket
class, this is becauseMessageWebSocket.ConnectAsync
method requires a URI parameter as you provided.WebSockets
provide a mechanism for fast, secure two-way communication between a client and a server over the web using HTTP(S).Socket
can be used for communicating with other device. They are not the same things, we cannot simply transferWebSockets
toStreamSocket
. More details aboutWebSockets
andStreamSockets
in uwp please reference the official documentsSockets
andWebSockets
.But
WebSockets
can 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.