XSockets.Net Server onOpen事件实现

本文关键字:事件 实现 onOpen Server Net XSockets | 更新日期: 2023-09-27 18:11:38

我调整XSockets。

我读http://xsockets.net/docs/c-server-api#using_events

使用事件

服务器端API提供了一些事件来帮助你。

OnOpen

当客户端连接并握手完成时调用。

//Hook up the event in the constructor.
public MyController()
{
    this.OnOpen += MyController_OnOpen;
}
void MyController_OnOpen(object sender, OnClientConnectArgs e)
{
    //The connection is open...
}

那么,如果我在visualstudio.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_OnOpenOnClientConnectArgs用红色下划线警告,这显然意味着不好,不能工作。

我以前是一个不错的c#程序员,现在我做node.js。

我知道他们想做的是

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

很简单,但是我不知道如何在c#中做到这一点。

你能告诉我吗?谢谢!

XSockets.Net Server onOpen事件实现

猜测您正在使用XSockets 3.0.6 ?但是我将为3.0.6和新的4.0编写示例

真的不知道从onOpen发送一个连接的事件,因为服务器会自动发送一个事件…

控制器的名字"Server"让我很困惑,不管怎样,看下面。

服务器端

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");
    }
}
客户端

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();

所以3。*和4。*是你可以在一个套接字上的多个控制器上复用。

4.0将支持RPC,这使得绑定到"connected"已经过时了…我们可以直接从服务器调用该方法,使用"Invoke"

猜测你正在用NodeJS编写一些自定义客户端,但你仍然会收到OnOpen数据,而不需要自己从控制器上的OnOpen事件发送它!

的问候Uffe

我不得不做

  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)不充分,导致错误发生