Jay
Jay

Reputation: 5084

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

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.

Upvotes: 1

Views: 1181

Answers (1)

BNK
BNK

Reputation: 24114

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

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!

Upvotes: 3

Related Questions