ASP.Net Error: Error during WebSocket handshake: Unexpected response code: 404

Last Reply 10 days ago By dharmendr

Posted 10 days ago

 have some code in mvc which are given error WebSocket connection to 'ws://localhost/api/Chatl?userid=12' failed: Error during WebSocket handshake: Unexpected response code: 404 how could resolve it??

 

 this is web api Controller file

using Microsoft.Web.WebSockets;
using System;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

namespace socket_test
{
    public class ChatController : ApiController
    {
    //  [Route("api/WsHandler")]
        [HttpGet]
        public HttpResponseMessage Get(string userid)
        {
            HttpContext.Current.AcceptWebSocketRequest(new ChatWebSocketHandler(userid));
            return Request.CreateResponse(HttpStatusCode.SwitchingProtocols);
        }
        class ChatWebSocketHandler : WebSocketHandler
        {
            private static WebSocketCollection _chatClients = new WebSocketCollection();
            private string _userid;
            public ChatWebSocketHandler(string userid)
            {
                _userid = userid;
            }
            public override void OnOpen()
            {
                _chatClients.Add(this);
            }
            public override void OnMessage(string message)
            {
                _chatClients.Broadcast(_userid + ": " + message);
            }
        }
    }
}

 And this is chat.html file 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <!--<script src="Scripts/Socket.js"></script>-->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var userid = prompt('Please enter a userid:');
            var uri = 'ws://' + window.location.hostname + window.location.pathname.replace('chat.htm', 'api/Chat') + '?userid=' + userid;
            websocket = new WebSocket(uri);
            websocket.onopen = function () {
                $('#messages').prepend('<div>Connected.</div>');
                $('#chatform').submit(function (event) {
                    websocket.send($('#inputbox').val());
                    $('#inputbox').val('');
                    event.preventDefault();
                });
            };
            websocket.onerror = function (event) {
               
                    $('#messages').prepend('<div>ERROR</div>');
            
            };
           
            websocket.onmessage = function (event) {
                $('#btn').click(function () {
                    $('#messages').prepend('<div>' + event.data + '</div>');
                });
            };
        });
    </script>
</head>
<body>
    <form id="chatform" action="">
        <input id="inputbox" />
        <input type="button" id="btn" value="BroadCast" />
    </form>
    <div id="messages" />
</body>
</html>

 

You are viewing reply posted by: dharmendr 10 days ago.