statianzo / Fleck

C# Websocket Implementation
MIT License
2.28k stars 583 forks source link

Using WebSockets with ASP.NET MVC 5 on Azure #278

Open gauravshrestha opened 4 years ago

gauravshrestha commented 4 years ago

Hello,

I am using ASP.NET MVC 5 and have successfully tested Fleck on my local machine. I deployed this application to an app service on Azure and I can't make a web socket connection. Here is what I have on the Application_Start() :


void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);

            // OPEN A WEBSOCKET CONNECTION.
            FleckLog.Level = LogLevel.Debug;
            var allSockets = new Dictionary<string, IWebSocketConnection>();
            var server = new WebSocketServer("ws://0.0.0.0:8080");
            server.Start(socket =>
            {
                socket.OnOpen = () =>
                {
                    //Console.WriteLine("Open!");
                    var guid = socket.ConnectionInfo.Path.Replace("/", "");
                    allSockets.Add(guid, socket);
                };
                socket.OnClose = () =>
                {
                    //Console.WriteLine("Close!");
                    var guid = socket.ConnectionInfo.Path.Replace("/", "");
                    allSockets.Remove(guid);
                };
                socket.OnMessage = message =>
                {
                    //Console.WriteLine(message);
                    allSockets.Values.ToList().ForEach(s => s.Send("Echo: Sending back the same message: " + message));
                };

                socket.OnBinary = (bytes) =>
                {
                    //Console.WriteLine("received binary");
                };
            });        
        }

On the client side, here is what I have:

<script type="text/javascript">
        var ws = null;
        function createConnection() {
            ws = new WebSocket("ws://my-test-app.azurewebsites.net:8080");
            ws.binaryType = "blob";
            ws.onopen = function () {
                alert("About to send data");
                ws.send("Hello World"); // I WANT TO SEND THIS MESSAGE TO THE SERVER!!!!!!!!
                alert("Message sent!");
            };

            ws.onmessage = function (evt) {
                alert("About to receive data");
                var received_msg = evt.data;
                alert("Message received = "+received_msg);
            };
            ws.onclose = function () {
                // websocket is closed.
                alert("Connection is closed...");
            };
        };

        function sendMessage() {
            var txtBox = document.getElementById('messageTxt');
            var txtBox2 = document.getElementById('messageTxt2');

            const arr = [txtBox.value, txtBox2.value, 3, 5];

            //const array = new Float32Array(5);

            //for (var i = 0; i < array.length; ++i) {
            //    array[i] = i / 2;
            //}
            //console.log(array);
            ws.send(arr);
        }
    </script>

When trying to connect, I get the following error: WebSocket connection to 'ws://my-test-app.azurewebsites.net:8080/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

Any suggestions will be highly appreciated.

Thank you.

AdrianBathurst commented 4 years ago

Most likely the ip the socket server is listening on, try changing the socket server ip to your domains ip address 92.242.132.24 instead of 0.0.0.0