2

Can anybody give me a working example of an ASP dot net 4.0 website project that has a working implementation of XSockets in it?

I have set up a blank solution and can connect to ws://localhost:4502/Generic fine, but I cannot connect to a custom controller added to the App_Code folder.

I am using the latest XSockets, installed via nuget today (13 Jan 2014).

When trying to connect to the custom controller I get an XSocketException "The handler name was not found in loaded plugins".

The custom controller code is I file called MyTestController.cs, and in App_Code folder. I have been trying to get it to work for 4-5 hours, with no luck. I cannot find any documentation that says I need to register the new controllers manually in the RegisterRoutes call.

I have tried to create an MVC app to see what adding XSockets does to it but this did not manually register any routes.

Using a Web Application or an MVC application is not possible as we need to try to implement this into a very big and mature legacy website.

The custom controller is as follows:

 using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Web;

using XSockets.Core.Common.Socket.Event.Interface;
using XSockets.Core.XSocket;
using XSockets.Core.XSocket.Helpers;


/// <summary>
/// Summary description for TestController
/// </summary>
/// 
namespace XSocketsTest1
{ 

    public class MyTestController: XSocketController
    {
        public MyTestController()
        {
            Debug.Print("TestController Created");
            this.OnOpen += TestController_OnOpen;
            this.OnClose += TestController_OnClose;
        }

        void TestController_OnClose(object sender, XSockets.Core.Common.Socket.Event.Arguments.OnClientDisconnectArgs e)
        {
            Debug.Print("TestController_OnClose");
            //throw new NotImplementedException();
        }

        void TestController_OnOpen(object sender, XSockets.Core.Common.Socket.Event.Arguments.OnClientConnectArgs e)
        {
            Debug.Print("TestController_OnOpen");
            this.Send("Hello there. You have connected.", "msgClient");
        }   

        void RunReport()
        {
            Debug.Print("RunReport");
            this.Send("You called RunReport", "msgClient");
        }
    }
}

Default.aspx is here:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Clean Page - XSockets In Web Site Project Tester</h1>
        <p>This is to prototype xsockets in a web site project.</p>
        <p>Target environment needs to be .net 4.0 Web Site project, and <em>cannot be a web application</em>.</p>

        <h4>Results</h4>
        <div class="js-log-contents log-contents">
        </div>

    </div>
    </form>

    <style type="text/css">
        .log-contents{border:2px solid grey;min-height:200px; width:400px;}
        .log-contents.success{border-color:#0f0;}
        .log-contents.error{border-color:#f00;}
        em{font-weight:bolder;}
    </style>
    <script src="Scripts/jquery-2.0.3.min.js"></script>
    <script src="Scripts/XSockets.latest.js"></script>
    <script src="Scripts/XSockets.fallback.latest.js"></script>
    <script type="text/javascript">
        var page =
            {
                self: this,
                config: {
                    //controllerAddress: "ws://localhost:4502/MyTest"                                                //  Does Not Work
                    controllerAddress: "ws://localhost:4502/MyTestController"                              //  Does Not Work
                    //controllerAddress: "ws://localhost:4502/XSocketsTest1.MyTest"                      //  Does Not Work
                    //controllerAddress: "ws://localhost:4502/XSocketsTest1.MyTestController"   //  Does Not Work
                    //controllerAddress: "ws://localhost:4502/Generic"                                               //  Works
                },

                logger:
                {
                    logIdentifier: "js-log-contents",
                    failCssClass: "error",
                    successCssClass: "success",
                    fail: function (msg) { $("." + this.logIdentifier).append(msg).removeClass(this.successCssClass).addClass(this.failCssClass); },
                    success: function (msg) { $("." + this.logIdentifier).append(msg).removeClass(this.failCssClass).addClass(this.successCssClass); }
                }
            };


    //  XSockets client code - copied from xsockets.net.
        var conn;
        var controllerAddress = page.config.controllerAddress;
        conn = new XSockets.WebSocket(controllerAddress);

        conn.on(XSockets.Events.open, function (clientInfo) {
            console.log("Open successful, controller name is " + controllerAddress, clientInfo)
            page.logger.success("Successfully opened " + controllerAddress);
        });

        conn.on(XSockets.Events.onError, function (err) {
            console.log('Error, controller name is ' + controllerAddress, err);
            page.logger.fail("Failed to open " + controllerAddress);
        });

        $(function () {
        });
    </script>
</body>
</html>
4

2 回答 2

2

当您将类添加到 App_Code 文件夹时,Visual Studio 会将它们标记为“build action = content”而不是“build action = compile”如果您想让控制器在 App_Code 中,只需右键单击文件并选择属性,然后将构建操作更改为编译而不是内容。

尽管 XSockets 具有 MVC 模式,但它与 ASP.NET MVC 无关,因此您不会看到任何路由。XSockets 是独立构建的,不依赖于 IIS、Apache 等。

由于您在 App_Code 下的类(控制器)没有编译 XSockets 在启动时不会找到它。如果由于某种原因您不能更改为“构建操作”= 编译,那么添加一个类库来安装 XSockets.Core 并构建您的控制器。然后,您可以在启动 XSockets 服务器的项目中引用此库。现在它将被 XSockets 找到。

顺便提一句。我看到您在客户端连接时发送了“msgClient”事件,但我在您的 javascript 中看不到该主题的任何订阅。

于 2014-01-14T07:56:05.643 回答
0

有没有人想出如何更改构建操作或“添加一个类库来安装 XSockets.Core 并构建你的控制器。然后你可以在启动 XSockets 服务器的项目中引用这个库。现在 XSockets 将找到它。” 那..

据我所知,您无法更改 asp.net 网页项目中文件的构建操作,我什至不确定如何添加类库来安装 XSockets.Core 和构建控制器.. xD

于 2014-06-16T15:43:36.243 回答