Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to set up a WebSocket for Azure

$
0
0

I have a Blazor project in Visual Studio 2022. It runs fine both locally and on Azure cloud (I publish it from Visual Studio).

I wanted to add a WebSocket server to this app. (I have previously done this on Azure but using Node, not C#)

I am using websocket-sharp (websocket-sharp)

Locally it all works fine.

I am creating the server in one of my Blazor service .cs files like so:

m_WSServer = new WebSocketServer(3333, false);

and adding a service to this like so:

m_WSServer.AddWebSocketService("/" +"Laputa");

and this service just returns a message when it receives one, like so:

Send("Elvis Lives!");

Now, in my .razor file I am creating the client like so (where 11.22.33.44 is the IP address on my network of the machine which the web socket server is running on):

m_WSClient = new WebSocket("ws://11.22.33.44:3333/Laputa");

I connect like so:

m_WSClient.Connect();

Send a message like so:

m_WSClient?.Send("Test");

and I receive "Elvis Lives!" in my OnMessage listener.

Now, if I want to publish this to azure and have it work how should I change the lines which create the server and the client?

(I have enabled web sockets on my Azure app from the Azure homepage).

Azure requires secure socket connections, so I changed the creation line to:

m_WSServer = new WebSocketServer(3333, true);

and of course, the client can no longer use 11.22.33.44 or ws, so I changed this line to:

m_WSClient = new WebSocket("wss://myazureappname.azurewebsites.net/Laputa");

But this does not work, client does not connect.

Is that client url correct, (i.e. same as url of my web app but with wss instead of http)?

and maybe I need to change the server creation line? At the moment it just specifies the port and 'true', there is an overload that takes a URL).

Thanks for any help.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>