XSockets.Net Server onOpen event implementation

拜拜、爱过 提交于 2020-01-26 03:40:34

问题


I tweak XSockets.Net Server now.

I read http://xsockets.net/docs/c-server-api#using_events

Using events

The server side API offer a few events to help you out.

OnOpen

Invoked when the client is connected and the handshake is completed.

//Hook up the event in the constructor.
public MyController()
{
    this.OnOpen += MyController_OnOpen;
}

void MyController_OnOpen(object sender, OnClientConnectArgs e)
{
    //The connection is open...
}

so, if I do the below in VisualStduio.net

  class MyController : XSocketController
        {
            //Hook up the event in the constructor.
            public MyController()
            {
                this.OnOpen += MyController_OnOpen;
            }

            void MyController_OnOpen(object sender, OnClientConnectArgs e)
            {
                //The connection is open...
            }

        }

MyController_OnOpen and OnClientConnectArgs is cautioned with red underline, which obviously means no good and not going to work.

I was an OK C# coder before, and now I do node.js.

I know what they try to do is

var myController = XSocketCotroller();

var myController_onOpen = function(obj,e)
                        {
                           // The connection is open...
                        };

myControler.onOpen = myController_onOpen;  

simple enough, but I don't know how to do this in C#.

Could you instruct. Thanks!


回答1:


Guessing that you are using XSockets 3.0.6 ? But I will write samples for both 3.0.6 and the new 4.0

There is really no idea sending a connected event from the onOpen since the server will send a event automatically...

The name of the controller you have "Server" confuses me, anyway see below.

Server side

3.0.6

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

namespace KenOkabe
{
    public class SampleController : XSocketController
    {
        public SampleController()
        {
            this.OnOpen += SampleController_OnOpen;
        }

        void SampleController_OnOpen(object sender, OnClientConnectArgs e)
        {
            //Send connected topic to client that connected.
            //Just passing a anonymous object with info about the client, but it can be anything serializable..
            this.Send(new {this.ClientGuid, this.StorageGuid},"connected");
        }
    }
}

4.0

public class SampleController : XSocketController
{
    public SampleController()
    {
        this.OnOpen += SampleController_OnOpen;
    }

    void SampleController_OnOpen(object sender, OnClientConnectArgs e)
    {
        //Send connected topic to client that connected.
        //Just passing a anonymous object with info about the client, but it can be anything serializable..
        this.Invoke(new { this.ConnectionId, this.Context.PersistentId }, "connected");
    }
}

Client side

3.0.6

var client = new XSocketClient("ws://127.0.0.1:4502/SampleController", "*");
client.OnOpen += (sender, eventArgs) => { Console.WriteLine("OPEN"); };
client.Bind("connected", textArgs => Console.WriteLine(textArgs.data));
client.Open();

4.0

var client = new XSocketClient("ws://127.0.0.1:4502", "http://localhost", "SampleController");
client.Controller("SampleController").OnOpen += (sender, connectArgs) => Console.WriteLine("OPEN");
client.Controller("SampleController").On("connected",data => Console.WriteLine(string.Format("{0}, {1}", data.ConnectionId, data.PersistentId)));
client.Open();

So the big difference between 3.* and 4.* is that you can multiplex over several controllers on one socket.

4.0 will support RPC and that makes the binding to "connected" obsolete... We can just call the method directly from the server using "Invoke"

Guessing that you are writing some custom client in NodeJS, but you will still receive the OnOpen data without sending it yourself from the OnOpen event on the controller!

Regards Uffe




回答2:


I had to do

  public class Server : XSocketController
    {
        public Server()
        { 
            this.OnOpen += onOpen; 
        }

        void onOpen(object sender, XSockets.Core.Common.Socket.Event.Arguments.OnClientConnectArgs e)
        {
            this.send("connected");
        }
        public void send(String msg)
        {
            this.send(msg);
        }

    }

OnClientConnectArgs e) is not sufficient that occurs the error.



来源:https://stackoverflow.com/questions/24573079/xsockets-net-server-onopen-event-implementation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!