我现在调整了XSockets.Net服务器。
我读过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...
}所以,如果我在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被警告使用红色下划线,这显然意味着没有好处,也没有效果。
我以前是一个OK的C#编码器,现在我做node.js。
我知道他们想做的是
var myController = XSocketCotroller();
var myController_onOpen = function(obj,e)
{
// The connection is open...
};
myControler.onOpen = myController_onOpen; 很简单,但我不知道如何在C#中做到这一点。
你能告诉我。谢谢!
发布于 2014-07-05 10:25:04
猜测您使用的是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();因此,3.*和4.*之间的最大区别是,您可以在一个套接字上对多个控制器进行多路复用。
4.0将支持RPC,这使得“连接”的绑定过时了.我们可以使用“调用”直接从服务器调用该方法。
猜测您正在用NodeJS编写一些自定义客户端,但是您仍将接收OnOpen数据,而不会自己从控制器上的OnOpen事件中发送数据!
问候乌夫
发布于 2014-07-05 04:33:56
我不得不做
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)是不够的。
https://stackoverflow.com/questions/24573079
复制相似问题