I created a Blazor project in asp.net core 3.1 and added API support and now was in process of adding Websockets to it.
I followed this tutorial.
https://dotnetplaybook.com/which-is-best-websockets-or-signalr/
Although the websocket middleware and everyting works, the moment I integrated it into Blazor project I am having issues
Hello from 2nd Request Delegate - No WebSocket
WebSocketServerConnectionManager-> AddSocket: WebSocket added with ID: 18a403b3-cbab-4523-b25d-00cddd4f05d5
Receive->Text
Message: {"protocol":"blazorpack","version":1}
Unhandled exception. Receive->Close on: 18a403b3-cbab-4523-b25d-00cddd4f05d5
Managed Connections: 0
Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content: . Path '', line 1, position 37.
at Newtonsoft.Json.JsonTextReader.Read()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
Apparently there seems to be a websocket connection made to the server (even though I did not do anything) as soon as I load the webpage by blazor itself.
The socket message being ==> {"protocol":"blazorpack","version":1}
Of course the code is not handling this format and hence the error (which can be fixed).
But I am curious about this automatic socket connection made to begin with. Anyone know more about the mystery in-built socket messages from Blazor template?
Using Websockets (through SignalR) is how Blazor passes information between the client part and the server part. It's fundamental to the entire architecture of the platform. It can fall back to http long-polling but this isn't recommended. See https://learn.microsoft.com/en-us/aspnet/core/blazor/host-and-deploy/server?view=aspnetcore-3.1#signalr-configuration .
I doubt if you need a specific separate websocket or signalr implementation on top of that (unless it's to connect to a different server / application separate from the Blazor app).