Sharing SignalR hub with ASP.NET Website and a Web API Mobile Backend

旧街凉风 提交于 2019-12-08 05:17:10

问题


I've set up SignalR Realtime communication for my ASP.NET based website. However, I need to know if it's possible to share the same SignalR Hub with a separate Mobile Backend Project which deals with the same server and database. Basically like accessing the facebook's inbox through web and mobile at the same time?

That would be a ASP.NET Web project and a Mobile Client connected to one hub.


回答1:


If you want to find SignalR for Android, I suggest the following working link for your to start

  • SignalR/java-client at GitHub

You can refer to the following sample code (this is from my question on SO about SignalR for Android: SignalR for Android: how can I pass dynamic class to SubscriptionHandler1

public <T> void startSignalR(String transport, String serverUrl, final String userName, final Class<T> tClass) {

    Platform.loadPlatformComponent(new AndroidPlatformComponent());
    Credentials credentials = new Credentials() {
        @Override
        public void prepareRequest(Request request) {
            request.addHeader(HEADER_KEY_USERNAME, userName);
        }
    };

    mConnection = new HubConnection(serverUrl);
    mConnection.setCredentials(credentials);
    mHub = mConnection.createHubProxy(SERVER_HUB_CHAT);

    if (transport.equals("ServerSentEvents")) {
         mTransport = new ServerSentEventsTransport(mConnection.getLogger());
    } else if (transport.equals("LongPolling")) {
         mTransport = new LongPollingTransport(mConnection.getLogger());
    }

    mAwaitConnection = mConnection.start(mTransport);

    try {
        mAwaitConnection.get();
    } catch (InterruptedException e) {      
        e.printStackTrace();      
        return;
    } catch (ExecutionException e) { 
        e.printStackTrace();
        return;
    }

    mHub.on("broadcastMessage",
            new SubscriptionHandler1<Object>() {
                @Override
                public void run(final Object msg) {
                    final String finalMsg;
                    Gson gson = new Gson();
                    Object object = gson.fromJson(msg.toString(), tClass);
                    Field[] fields = object.getClass().getDeclaredFields();
                    for (int i = 0; i < fields.length; i++) {
                        try {
                            System.out.println("Value = " + fields[i].get(object));                                
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }                        
                }
            }
            , Object.class);

    ...
}

Hope this helps!



来源:https://stackoverflow.com/questions/32288357/sharing-signalr-hub-with-asp-net-website-and-a-web-api-mobile-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!