2014-09-02 44 views
3

我在下面的方法都試過了,實現信號R在android系統

import java.util.concurrent.ExecutionException; 

import microsoft.aspnet.signalr.client.LogLevel; 
import microsoft.aspnet.signalr.client.Logger; 
import microsoft.aspnet.signalr.client.Platform; 
import microsoft.aspnet.signalr.client.SignalRFuture; 
import microsoft.aspnet.signalr.client.http.android.AndroidPlatformComponent; 
import microsoft.aspnet.signalr.client.hubs.HubConnection; 
import microsoft.aspnet.signalr.client.hubs.HubProxy; 
import android.app.Activity; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.util.Base64; 

public class HomeActivity2 extends Activity { 
    /** 
    * shared preference for access temp storage 
    */ 
    private SharedPreferences settings; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     settings = getSharedPreferences(Utils.PREFS_NAME, 0); 
     Platform.loadPlatformComponent(new AndroidPlatformComponent()); 
     String host = "MyURL"; 
     String CONNECTION_QUERYSTRING = "ClientID=" 
       + settings.getString("clientID", "") + "&RoleID=" 
       + settings.getString("roleID", "") + "&UserID=" 
       + settings.getString("employeeID", ""); 
     HubConnection connection = new HubConnection(host, 
       Base64.encodeToString(CONNECTION_QUERYSTRING.getBytes(), 
         Base64.URL_SAFE | Base64.NO_WRAP), false, new Logger() { 

        @Override 
        public void log(String message, LogLevel level) { 
         // TODO Auto-generated method stub 
         System.out.println(message); 
        } 
       }); 
     HubProxy hub = connection.createHubProxy("NotificationHub"); 
     SignalRFuture<Void> awaitConnection = connection.start(); 
     try { 
      awaitConnection.get(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

,這是我的logcat跟蹤

HubConnection - Creating hub proxy: notificationhub 
HubConnection - Entered startLock in start 
HubConnection - Start the connection, using AutomaticTransport transport 
HubConnection - Start negotiation 
AutomaticTransport - Start the negotiation with the server 
HubConnection - Getting connection data: [{"name":"notificationhub"}] 
HubConnection - Getting connection data: [{"name":"notificationhub"}] 
AutomaticTransport - Execute the request 
Create new thread for HTTP Connection 
Execute the HTTP Request 
URL: http://citrusz.com/notification/signalr/negotiate?clientProtocol=1.3&connectionData=%5B%7B%22name%22%3A%22notificationhub%22%7D%5D&Q2xpZW50SUQ9Q0xOVEFBQUEwMDAxJlJvbGVJRD1ST0xFQUFBQTAwMDEmVXNlcklEPUVNUEFBQUEwMDAz 
VERB: GET 
Header User-Agent: SignalR (lang=Java; os=android; version=2.0) 
CONTENT: null 
Request executed 
AutomaticTransport - Response received 
AutomaticTransport - Read response data to the end AutomaticTransport - Trigger onSuccess with negotiation data: {"Url":"/notification/signalr","ConnectionToken":"SYqo8IyKwjb1zqPzDkPuVsMSrqgDmaQASB0Jirr1yUXRW698WbS8cM0BYuHdFQIEtQf5IYenCNp+1KV2EwUF7QOAcyaLbI4ohLiGKvf2umGn6+dbitwZcKLwjSCgJfpo","ConnectionId":"41c2e849-756f-4cb9-90fc-2688fdbbb619","KeepAliveTimeout":20.0,"DisconnectTimeout":30.0,"TryWebSockets":false,"ProtocolVersion":"1.3","TransportConnectTimeout":5.0} 
HubConnection - Negotiation completed 
HubConnection - ConnectionId: 41c2e849-756f-4cb9-90fc-2688fdbbb619 
HubConnection - ConnectionToken: SYqo8IyKwjb1zqPzDkPuVsMSrqgDmaQASB0Jirr1yUXRW698WbS8cM0BYuHdFQIEtQf5IYenCNp+1KV2EwUF7QOAcyaLbI4ohLiGKvf2umGn6+dbitwZcKLwjSCgJfpo 
HubConnection - Keep alive timeout: 20.0 
HubConnection - Entered startLock in startTransport 
HubConnection - Starting the transport 
GC_CONCURRENT freed 620K, 12% free 6348K/7175K, paused 3ms+3ms 
HubConnection - Starting transport for InitialConnection 
serverSentEvents - Start the communication with the server 
HubConnection - Getting connection data: [{"name":"notificationhub"}] 
serverSentEvents - Execute the request 
Create new thread for HTTP Connection 
Execute the HTTP Request 
URL: http://citrusz.com/notification/signalr/connect?transport=serverSentEvents&connectionToken=SYqo8IyKwjb1zqPzDkPuVsMSrqgDmaQASB0Jirr1yUXRW698WbS8cM0BYuHdFQIEtQf5IYenCNp%2B1KV2EwUF7QOAcyaLbI4ohLiGKvf2umGn6%2BdbitwZcKLwjSCgJfpo&connectionId=41c2e849-756f-4cb9-90fc-2688fdbbb619&connectionData=%5B%7B%22name%22%3A%22notificationhub%22%7D%5D&Q2xpZW50SUQ9Q0xOVEFBQUEwMDAxJlJvbGVJRD1ST0xFQUFBQTAwMDEmVXNlcklEPUVNUEFBQUEwMDAz 
VERB: GET 
Header Accept: text/event-stream 
Header User-Agent: SignalR (lang=Java; os=android; version=2.0) 
CONTENT: null 
Request executed 
threadid=3: reacting to signal 3 
Wrote stack traces to '/data/anr/traces.txt' 

我覺得SignalR連接,但我的Android螢幕全黑,如果我評論awaitConnection.get();應用程序工作正常,我不知道我做錯了什麼。任何幫助將是非常可觀的

感謝,古納

+0

有同樣的問題在: HubConnection - 獲取連接數據:[{ 「Name」: 「notificationhub」}] 鎖定整個應用過程.. – 2015-01-22 17:35:45

回答

3

它掛,你是通過調用進行連接同步「獲得()」。

我遇到了類似的問題,在我的情況下,這是一個WebSockets的問題,必須回退到使用ServerSentEvents。

HubProxy hub = connection.createHubProxy("NotificationHub") 
ClientTransport transport = new ServerSentEventsTransport(connection.getLogger()); 

SignalRFuture<Void> awaitConnection = connection.start(transport); 
try { 
    awaitConnection.get(); 
} 
catch (InterruptedException e) { 
    e.printStackTrace(); 
} catch (ExecutionException e) { 
    e.printStackTrace(); 
} 
0

您可能需要仔細檢查一下您的開發環境以確保WebSockets得到支持。 Android設備支持WebSocket;但是,您的服務器可能不會。

WebSockets在Windows 8/Server 2012及更高版本的IIS 8中開始受到支持。

的選項有:

  • 地方發展&測試,至少的Visual Studio 2013上 Windows 8.1中使用。
  • 對於生產,使用至少是Windows Server 2012 * 與IIS 8

* 要啓用該功能,你可能需要去到Windows角色&功能 - >網絡 服務器 - >應用程序開發 - >安裝WebSockets。

+0

「只有Windows Server 2012上,iis8上支持WebSocket的,太win8的本地主機iisexpress「這是不正確的 – beresfordt 2015-03-17 20:15:54