Guest User

Guest User

  • Tech Writer
  • 82
  • 6.6k

c sharp with socket programming

Sep 5 2019 3:50 AM
i 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 chat.html file 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <title></title>  
  6.     <!--<script src="Scripts/Socket.js"></script>-->  
  7.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>  
  8.     <script type="text/javascript">  
  9.         $(document).ready(function () {  
  10.             var userid = prompt('Please enter a userid:');  
  11.             var uri = 'ws://' + window.location.hostname + window.location.pathname.replace('chat.htm', 'api/Chat') + '?userid=' + userid;  
  12.             websocket = new WebSocket(uri);  
  13.             websocket.onopen = function () {  
  14.                 $('#messages').prepend('<div>Connected.</div>');  
  15.                 $('#chatform').submit(function (event) {  
  16.                     websocket.send($('#inputbox').val());  
  17.                     $('#inputbox').val('');  
  18.                     event.preventDefault();  
  19.                 });  
  20.             };  
  21.             websocket.onerror = function (event) {  
  22.                  
  23.                     $('#messages').prepend('<div>ERROR</div>');  
  24.               
  25.             };  
  26.              
  27.             websocket.onmessage = function (event) {  
  28.                 $('#btn').click(function () {  
  29.                     $('#messages').prepend('<div>' + event.data + '</div>');  
  30.                 });  
  31.             };  
  32.         });  
  33.     </script>  
  34. </head>  
  35. <body>  
  36.     <form id="chatform" action="">  
  37.         <input id="inputbox" />  
  38.         <input type="button" id="btn" value="BroadCast" />  
  39.     </form>  
  40.     <div id="messages" />  
  41. </body>  
  42. </html>  
this is webapi controller file 
  1. using Microsoft.Web.WebSockets;  
  2. using System;  
  3. using System.Net;  
  4. using System.Net.Http;  
  5. using System.Web;  
  6. using System.Web.Http;  
  7.   
  8. namespace socket_test  
  9. {  
  10.     public class ChatController : ApiController  
  11.     {  
  12.     //  [Route("api/WsHandler")]  
  13.         [HttpGet]  
  14.         public HttpResponseMessage Get(string userid)  
  15.         {  
  16.             HttpContext.Current.AcceptWebSocketRequest(new ChatWebSocketHandler(userid));  
  17.             return Request.CreateResponse(HttpStatusCode.SwitchingProtocols);  
  18.         }  
  19.         class ChatWebSocketHandler : WebSocketHandler  
  20.         {  
  21.             private static WebSocketCollection _chatClients = new WebSocketCollection();  
  22.             private string _userid;  
  23.             public ChatWebSocketHandler(string userid)  
  24.             {  
  25.                 _userid = userid;  
  26.             }  
  27.             public override void OnOpen()  
  28.             {  
  29.                 _chatClients.Add(this);  
  30.             }  
  31.             public override void OnMessage(string message)  
  32.             {  
  33.                 _chatClients.Broadcast(_userid + ": " + message);  
  34.             }  
  35.         }  
  36.     }  
  37. }  

Answers (1)