1

I'm trying to set up a specific scenario but, obviously, I'm having problems. My server is a site that primarily hosts a WCF service but I want to add an XSockets host there as well. I have the standard code in the bootstrap code file as per the instructions in the readme.txt. Upon a client connection, I am starting a worker thread which is basically a heartbeat that the client will monitor. The relevant code from the controller is as follows:

public class HeartbeatController : XSocketController
{
    public void AddMessage(string message)
    {
        this.SendToAll(message, "addMessage");
    }
}

Within my worker thread I am calling this:

string message = String.Format("pump", Math.Round(cpuCounter.NextValue());
ClientPool connection = ClientPool.GetInstance("ws://mywebsite:4502/HeartbeatController", "*");
connection.Send(message, "addMessage");

Currently I'm testing this with a console client which looks like this:

class Program
{
    static XSocketClient socketClient;

    static void Main(string[] args)
    {
        Console.WriteLine("Starting client...");

        string url = "ws://mywebsite:4502/HeartbeatController";

        socketClient = new XSocketClient(url, "*");
        socketClient.OnOpen += socketClient_OnOpen;
        socketClient.Open();

        while (true)
        {
            // let it sit and display the "pump" messages
            string input = Console.ReadLine();

            if (input.Equals("Q", StringComparison.Ordinal))
            {
                break;
            }
        }
    }

    static void socketClient_OnOpen(object sender, EventArgs e)
    {
        Console.WriteLine("socketClient Opened");
        socketClient.Bind("addMessage", OnAddMessage);
    }

    private static void OnAddMessage(ITextArgs textArgs)
    {
        Console.WriteLine("AddMessage :: {0}", textArgs.data);
    }
}

On the client, if I put a breakpoint in the socketClient_OnOpen method it gets hit so I think it is connecting. But the pump message never makes it to the client.

Two Questions:

  1. Is there anything obvious that I'm missing?
  2. (Unrelated) Since many enterprises really don't like punching holes in their firewalls, is there any way to use port 80 with this setup (so that the client connection would look like "ws://mywebsite/HeartbeatController")?

Thanks for any help!

4

1 回答 1

1

因此,为了查看您的泵实际发送到服务器的内容,我添加了一个自定义管道。

public class MyPipeline : XSocketPipeline
{
    //Incomming textmessage
    public override void OnMessage(IXSocketController controller, ITextArgs e)
    {
        Console.WriteLine("IN " + e.data);
        //Let the message continue into the server
        base.OnMessage(controller, e);
    }

    //Outgoing textmessage
    public override ITextArgs OnSend(IXSocketProtocol protocol, ITextArgs e)
    {
        Console.WriteLine("OUT " + e.data);
        return base.OnSend(protocol, e);
    }        
}

因为我随后看到您发送的字符串实际上没有名为“消息”的属性。动作方法“AddMessage”希望您传入字符串类型的属性消息。所以你可以通过两种方式解决这个问题,它们都很简单。

  1. 只需将 AddMessage 中的字符串参数替换为 ITextArgs

    public void AddMessage(ITextArgs message)
    

    或者...

  2. 从你的工作线程传入一个对象,而不是像这样的字符串

    connection.Send(new {message}, "addMessage");
    

所以你需要做的就是改变这一行

connection.Send(message, "addMessage");

与这一行

connection.Send(new {message}, "addMessage");

编辑:顺便说一句,4.0 即将推出,客户端以及服务器端的东西都会得到很大改进。

于 2014-05-26T19:20:58.213 回答