2014-12-01 2 views
0

다음과 같은 경우가 있습니다. 백그라운드에서 서비스로 Android 태블릿에서 실행되는 Websocket 서버입니다. 이 서비스는 서버가 인앱 통신을 처리하기 시작한 후에도 Websocket 클라이언트를 시작합니다. 나 또한 서버와 연결하고 통신하는 별도의 프로젝트에 자바 웹 소켓 클라이언트가있다.안드로이드 태블릿에서 Websocket 서버 서비스

WebSocket Server를 서비스로 구현하지 않는 한이 모든 것이 제대로 작동합니다. 내일은 없어도 데이터를 연결하고 보낼 수 있습니다.

하지만 Websocket 서버를 서비스로 실행하면 더 이상 연결할 수 없습니다. 내 "inAppClient"도 아니고 제 자바 클라이언트도 없습니다.

인터넷에 대한 사용 권한을 매니페스트에 설정하고 다른 포트를 시도했습니다. 나는 또한 adb forward TCP를 사용하여 portforwarding을한다 : 38301

내 인앱 클라이언트로 서버에 연결하려고하면 오류가 발생한다 : E/WSCLient : 연결에 실패했다. /127.0.0.1 (포트 38301) : 연결 실패 : ECONNREFUSED (연결이 거부 됨)

또한 127.0.0.1 대신 localhost를 사용하려고했습니다.

내 서버의 소스 코드입니다 :

private class WSServer extends WebSocketServer{ 
     private static final String TAG = "WSServer"; 
//  private static final int TIMEOUT = 60; 
     private int port = 38301; 
     private int testLevel = Constants.BOUNCE_MESSAGES; 

     public WSServer(InetSocketAddress address) { 
      super(address); 
      Log.d(TAG, "creating instance of local WSServer"); 
      wsClient = new WSClient(port); 
     } 

     public WSServer(int port) 
     { 
      super(new InetSocketAddress(port)); 
      Log.d(TAG, "creating instance of local WSServer on port " + port); 
      wsClient = new WSClient(port); 
     } 

     @Override 
     public void onOpen(WebSocket conn, ClientHandshake handshake) { 
      Log.d(TAG, "connection Details: " + conn.getLocalSocketAddress() + "; " + conn.getRemoteSocketAddress()); 
      Log.d(TAG, "Connection successfully opened on port: " + this.getPort()); 
     } 

     @Override 
     public void onClose(WebSocket conn, int code, String reason, boolean remote) { 
      Log.d(TAG, "Connection closed"); 
     } 

     @Override 
     public void onMessage(WebSocket conn, String message) { 
      Log.d(TAG, "Received Message: " + message); 
      if(testLevel == Constants.BOUNCE_MESSAGES) 
      { 
       conn.send(new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()) + ": " + message); 
      } 
     } 

//  @Override 
//  public void onFragment(WebSocket conn, Framedata fragment) { 
//   System.out.println("received fragment: " + fragment); 
//  } 

     @Override 
     public void onError(WebSocket conn, Exception ex) { 
      Log.d(TAG, "onError()", ex); 
     } 
} 

그리고 이것은 내 서비스의 소스 코드입니다 :

public CommManager(){ 
     wsServer = new WSServer(port); 
     Log.d(TAG, "Server Adress: " + wsServer.getAddress()); 
    } 

    public CommManager(int port){ 
     this.port = port; 
     Log.d(TAG, "CommManager is starting"); 
     wsServer = new WSServer(port); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Bundle extras = intent.getExtras(); 
     if(extras == null) { 
      Log.d(TAG, "Port wurde nicht gesetzt. Default Port " + port + " wird verwendet"); 
     } 
     else { 
      port = (Integer)extras.get("port"); 
     } 

     return startMode; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     Log.d(TAG, "Binding Service to Activity"); 
     return commManagerBinder; 
    } 

    @Override 
    public boolean onUnbind(Intent intent) { 

     return allowRebind; 
    } 

그리고 마지막으로 내에서-응용 프로그램 클라이언트 :

private class WSClient { 

     public String message; 
     private static final String TAG = "WSCLient"; 

     private WebSocketClient mWebSocketClient; 
     private int port = 38301; 

     public WSClient(){ 
      connectWebSocket(); 
     } 
     public WSClient(int port) { 
      Log.d(TAG, "starting local WSClient"); 
      if(port != 0){ 
       this.port = port; 
      } 
      connectWebSocket(); 
     } 

     private void connectWebSocket() { 
      Log.d(TAG, "establishing Connection to local WSServer"); 
      URI uri; 
      try { 
       uri = new URI("ws://localhost:" + port); 
      } catch (URISyntaxException e) { 
       Log.e(TAG, "uri-error: " + "ws://localhost:" + port); 
       e.printStackTrace(); 
       return; 
      } 

      mWebSocketClient = new WebSocketClient(uri) { 
       @Override 
       public void onOpen(ServerHandshake serverHandshake) { 
        Log.d(TAG, "Opened"); 
        mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL); 
       } 

       @Override 
       public void onMessage(String s) { 
        Log.d(TAG, "received Message: " + s); 
       } 

       @Override 
       public void onClose(int i, String s, boolean b) { 
        Log.d(TAG, "Closed " + s); 
       } 

       @Override 
       public void onError(Exception e) { 
        Log.e(TAG, "Error " + e.getMessage()); 
       } 
      }; 
      mWebSocketClient.connect(); 
     } 

     public void sendMessage(String message) { 

      if(mWebSocketClient != null) { 
       mWebSocketClient.send(message); 
      } 
      else { 
       Log.e(TAG, "No WebSocketClient available. Trying to reconnect and create new Client instance"); 
       connectWebSocket(); 
      } 
     } 

    } 

실마리가 없기 때문에 서버와의 연결을 설정할 수 없습니다. 도움을 받으실 수 있기를 바랍니다.

+0

나는 똑같은 것을 시도하고있다. WebSocketServer 클래스의 라이브러리를 물어봐도 될까요? – eskalera

+0

다음 라이브러리를 사용하고 있습니다 : https://github.com/TooTallNate/Java-WebSocket – Maverick1st

답변

0

그래서 구현 방법을 변경하여 문제를 해결했습니다. Server 클래스와 Client 클래스를 하위 클래스로 만든 다음 작동했습니다. :)

+0

전체 코드를 공유 할 수 있습니까? 비슷한 서비스를 구현하고 있습니다. – Chao

+0

죄송합니다. 내 직원을 변경 한 이후 더 이상 해당 코드에 액세스 할 수 없습니다. – Maverick1st

관련 문제