Question

I am using XSocket.Net for Real time Communication in .Net version 4.5.

I am very new to XSocket.Net.

The following is what i tried. In the Controller folder I created MyController with following code.

using System;
using XSockets.Core.XSocket;
using XSockets.Core.XSocket.Helpers;

namespace XSocketTest.Controllers
{
   public class MyController : XSocketController
   {

      public void OnChatMessage(string message)
      {
         this.SendTo( message, "onChatMessage");
      }
   }
}

The Following is the html page "index.html" in the root location

<!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<input type="text" id="input-message" value="Goo LR" />
<button id="btn-send">send</button>
<div id="messages"></div>
<script src="Scripts/jquery-2.1.0.min.js"></script>
<script src="Scripts/XSockets.latest.js"></script>

<script>
    var conn = null;
    $(function () {
        //Create a connection
        conn = new XSockets.WebSocket('ws://localhost:4023/My');
        conn.onopen = function () {


            conn.on('onchatmessage', function (d) {
                $('#messages').prepend($('<div>').text(d));
            });
        };

        $('#btn-send').on('click', function () {
            conn.publish('onchatmessage', { message: $('#input-message').val() });
        });
    });
  </script>
</body>
 </html>

I am getting an error in console(Chrome), on loading html, "WebSocket connection to 'ws://localhost:4023/My' failed: Error during WebSocket handshake: Unexpected response code: 404"

I have no idea what I am doing wrong.

Any help or sugggestions will be greatly appreciated.

Was it helpful?

Solution

Have you started the server?

My guess is that you have added the startup class (as described here see install into a webb-application).

Then you have the html file in the root, when requesting a non-serverside resource App_Start will not be called. In that case just do an initial request to the root and set a breakpoint in the startup class to see that it fires. Now you can go to the html file again.

Just saw another possible error, you are using port 4023, but did you start the server on that port? The default port is 4502, but you can ofcourse configure that (see here)

EDIT: There is also an error in the controller. It should not even compile I think. The row:

this.SendTo(message, "onChatMessage");

should probably be

this.SendToAll(message, "onChatMessage");

You can ofcourse use the SenTo but then you would have to pass in a filter

Func<T,bool>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top