gpt4 book ai didi

c# - SignalR 调用问题

转载 作者:太空宇宙 更新时间:2023-11-04 15:09:54 25 4
gpt4 key购买 nike

我使用 signalR android 实现: https://github.com/erizet/SignalA我在使用它时遇到一些问题。

我像这样初始化我的 HubConnection:

String url = "http://www.myurl.com";
con = new HubConnection(url, getActivity(), new LongPollingTransport())
{
@Override
public void OnStateChanged(StateBase oldState, StateBase newState) {
switch(newState.getState())
{
case Connected:

ArrayList<String> test = new ArrayList<String>();
test.add("header");
test.add("0");
test.add("data");
test.add("pleyh");

hub.Invoke("SendConnection", test, new HubInvokeCallback() {

@Override
public void OnResult(boolean arg0, String arg1) {
Log.i("HubConnection", "On result: arg0 = " + arg0 + " arg1 = " + arg1);
}

@Override
public void OnError(Exception arg0) {
Log.i("HubConnection", "On error: arg0 = " + arg0.getMessage());
}
});
break;
case Disconnected:
Log.i("HubConnection", "Disconnected");
stopSignalA();
break;
default:
break;
}
}

之后,我初始化 HubProxy 并开始连接:

try {
hub = con.CreateHubProxy("GameManager");

} catch (OperationApplicationException e) {
e.printStackTrace();
}
hub.On("addNewMessageToPage", new HubOnDataCallback()
{
@Override
public void OnReceived(JSONArray args) {
Log.i("HubConnection", "OnReceived " + args.toString());
for(int i=0; i<args.length(); i++)
{
Log.i("HubConnection", "OnReceived -> addNewMessageToPage = " + args.opt(i).toString());
}
}
});
con.Start();

似乎一切正常,因为协商成功并且我收到了带有 ConnectionToken 的 JSON。问题是当我收到 OnStateChanged 时进行调用时,当我知道我已连接时,

我收到此错误:

{"I":"0","E":"'SendConnection' method could not be resolved.","T":"   at

Microsoft.AspNet.SignalR.Hubs.NullMethodDescriptor.b_0(IHub emptyHub, Object[] emptyParameters)\r\n at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Incoming(IHubIncomingInvokerContext context)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.AspNet.SignalR.Hubs.HubPipelineModule.<>c_DisplayClass1.<b_0>d_3.MoveNext()"}

这是我发送到服务器的内容:

{"H":"gamemanager","A":["header","0","data","user"],"M":"SendConnection","I":"0"}

该错误表明方法“SendConnection”不存在,但它确实存在。有人已经遇到这个问题了吗?还是我的代码有问题?提前致谢!

编辑1:

集线器上的定义和方法定义:

public class GameManager : Hub
{
[HubMethodName("sendConnection")]
public void SendConnection(string Json)
{
Clients.Client(Context.ConnectionId).addNewMessageToPage("[SendConnection] Connection Established: " + Json);
}

[HubMethodName("sendToRoom")]
public void SendToRoom(string message)
{
Clients.Group(FindRoomFromConnectionId()._room).addNewMessageToPage("[sendToRoom]: " + message);
}
}

最佳答案

您可以粘贴您尝试调用的代码吗?看来您调用的方法名称有问题。还给出集线器方法定义。

关于c# - SignalR 调用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21474908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com