SignalR调用问题

时间:2014-01-31 07:48:21

标签: c# java android signalr

我使用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 ---从前一个位置开始的堆栈跟踪   抛出异常--- \ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)\ r \ n at   Microsoft.AspNet.SignalR.Hubs.HubPipelineModule&LT;。&以及c _DisplayClass1&LT; B_ 0&GT; d _3.MoveNext()“}

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

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

错误说“SendConnection”方法不存在,但确实存在。有人已经有这个问题吗?或者我的代码出了什么问题?提前谢谢!

修改1:

Hub和方法定义的定义:

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

2 个答案:

答案 0 :(得分:0)

是否可以粘贴您尝试调用的代码。似乎您调用的方法名称有问题。同时给出hub方法定义。

答案 1 :(得分:0)

我认为你在调用sendConnection时使用的参数太多了。尝试将代码更改为以下内容:

        ArrayList<String> test = new ArrayList<String>(1);
        test.add("only one argument");