我现在调整XSockets.Net服务器。
我看了http://xsockets.net/docs/c-server-api#using_events
服务器端API提供了一些事件来帮助您。
在连接客户端并完成握手时调用。
//Hook up the event in the constructor.
public MyController()
{
this.OnOpen += MyController_OnOpen;
}
void MyController_OnOpen(object sender, OnClientConnectArgs e)
{
//The connection is open...
}
所以,如果我在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
和OnClientConnectArgs
被警告用红色下划线,这显然意味着没有好处而且无法正常工作。
之前我是一个好的C#编码器,现在我做了node.js。
我知道他们试图做的是
var myController = XSocketCotroller();
var myController_onOpen = function(obj,e)
{
// The connection is open...
};
myControler.onOpen = myController_onOpen;
很简单,但我不知道如何在C#中做到这一点。
你可以指教吗?谢谢!答案 0 :(得分:0)
我不得不做
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)
是不够的。
答案 1 :(得分:0)
猜猜你在使用XSockets 3.0.6? 但我会为3.0.6和新的4.0编写样本
真的不知道从onOpen发送连接事件,因为服务器会自动发送事件......
你有“服务器”的控制器名称让我感到困惑,无论如何看到。
<强> 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();
因此,* *和4. *之间的最大区别在于,您可以在一个插槽上复用多个控制器。
4.0将支持RPC并且使得“已连接”的绑定过时...我们可以使用“Invoke”直接从服务器调用该方法
猜测你是在NodeJS中编写一些自定义客户端,但是你仍然会收到OnOpen数据,而不是自己从控制器上的OnOpen事件中发送它!
此致 Uffe