what version of windows are you using? be sure your version of IIS Express supports web sockets.
WebSocketException: Unable to connect to the remote server
I just started learning to write a server for my game with asp.net core.
I am trying to establish a websocket connection between the Unity game client and the asp.net core server.
I made a new asp.net core API project and added a new controller with this code in it.
(The code is copied from here)
namespace LazerbaseASPNET.Controllers
{
public class BrianWebSocketController : ControllerBase
{
[HttpGet("/ws")]
public async Task Get()
{
System.Diagnostics.Trace.TraceError("WEBSOCKET!!! GET!!! WEBSOCKET!!! GET!!! WEBSOCKET!!! GET!!! WEBSOCKET!!! GET!!! ");
if (HttpContext.WebSockets.IsWebSocketRequest)
{
using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
await Echo(webSocket);
}
else
{
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
}
}
private async Task Echo(WebSocket webSocket)
{
System.Diagnostics.Trace.TraceError("WEBSOCKET!!! ECHO!!!!! WEBSOCKET!!! ECHO!!!!! WEBSOCKET!!! ECHO!!!!! WEBSOCKET!!! ECHO!!!!! ");
var buffer = new byte[1024 * 4];
WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
while (!result.CloseStatus.HasValue)
{
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
}
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}
}
}
Startup.cs looks like this (I added app.UseWebSockets();)
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseWebSockets();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
In the Unity client I have the following code...
public async Task Connect ()
{
_socket = new ClientWebSocket();
Uri uri = new Uri(@"ws://localhost:44348/ws");
Debug.Log("start connecting websocket");
await _socket.ConnectAsync(uri, CancellationToken.None);
Debug.Log("finished connecting websocket");
Listen();
}
I press the run with IIS express button in visual studio, which opens a browser window at the local server, the address is https://localhost:44348.
Then I leave that browser open and run the code above in Unity.. Unity never gets to this second debug line.. Debug.Log("finished connecting websocket");
Also in the asp.net program visual studio output window I never see the WEBSOCKET!!! GET!!! log.
I get this error in Unity
WebSocketException: Unable to connect to the remote server
System.Net.WebSockets.WebSocketHandle+<ParseAndValidateConnectResponseAsync>d__28.MoveNext () (at <5a2009c85b134970925993880e2ecb2e>:0)
I have also tried publishing the asp.net program to azure and trying the url there, but I still never see the second debug.log in Unity.
I reckon my Unity code is fine as the same code is able to establish a socket connection and send and receive data from a node.js server I have on Heroku. I just have to change the url.
What am I doing wrong? Thanks!
2 answers
Sort by: Most helpful
-
-
B M 1 Reputation point
2022-08-01T23:25:53.343+00:00 So I got this working. There were two issues
- when I added these two attributes to my controller class with the sockets code in it, then I could successfully make a socket connection to server running locally from visual studio on my machine.
[ApiController] [Route("[controller]")] public class BrianWebSocketController : ControllerBase {
- It still did not work with the published version on azure however.. I searched all the options on azure and eventually found an option for websockets buried in there. It was turned off by default, so I turned it on and then I was able to make a connection to the server from Unity.
- when I added these two attributes to my controller class with the sockets code in it, then I could successfully make a socket connection to server running locally from visual studio on my machine.