Websocket, application doesn’t work properly in production env

  Kiến thức lập trình

I have application which is running in andorid (flutter). I have server part in .Net. Everything is working fine and I want to implement simple websocket so I use
web_socket_client like below:

Future<void> main() async {
  final appVersion = await getVersion();
  final backoff = LinearBackoff(
    initial: const Duration(seconds: 0),
    increment: const Duration(seconds: 1),
    maximum: const Duration(seconds: 5),
  );
  final socket = WebSocket(
    Uri.parse('ws://my_api_address/ws'),
    backoff: backoff,
  );

  socket.connection.listen((event) {
    if (event is Connected ||
        event is Reconnecting ||
        event is Reconnected ||
        event is Connecting) {
      runApp(
        StreamBuilder(
          stream: socket.messages,
          builder: (context, snapshot) {
            if (snapshot.hasData && snapshot.data != appVersion) {
              return RequiredVersionAlert(
                  appVersion: appVersion, requiredVersion: snapshot.data);
            }
            return const App();
          },
        ),
      );
    }
  });
}

There are two streams, first one for connection with server status, second one for message from socket. In the local env everything is forking fine, I didn’t spot any issues, but if I try to run in production env (.net app is deploing via IIS windows 10) there is something strange, the client (flutter) performance is very bad, application is stuttering and reacting very slow.

There is my code for websocket in server:

public class WebSocketController : ControllerBase
{
    private const string version = "3.0.0";
    [HttpGet("/ws")]
    public async Task Get()
    {
        if (HttpContext.WebSockets.IsWebSocketRequest)
        {
            using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
            await Echo(webSocket);
        }
        else
        {
            HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
        }
    }

    private static async Task Echo(WebSocket webSocket)
    {
        while (webSocket.State == WebSocketState.Open)
        {
            var versionBytes = Encoding.UTF8.GetBytes(version);
            await webSocket.SendAsync(new ArraySegment<byte>(versionBytes), WebSocketMessageType.Text, true, CancellationToken.None);
        }

        await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
    }
}

The websocket goal is simple, it’s just sending application version string.
Maybe there is some problem with server settings (firewall etc.)?

LEAVE A COMMENT