我有以下情况。Websocket服务器,在后台作为服务在Android平板电脑上运行。此服务还会在服务器启动后启动 Websocket 客户端以处理应用内通信。我在一个单独的项目中还有一个java web-socket客户端,用于连接服务器并与服务器通信。
这一切都可以正常工作,只要我不尝试将 WebSocket 服务器实现为服务。我可以连接和发送数据,就像没有明天一样。
但是一旦我将 Websocket 服务器作为服务运行,我就无法再连接到它。无论是我的"inAppClient"还是我单独的Java客户端。
我在清单中设置了互联网的权限,并尝试了不同的端口。我也使用 adb 转发进行端口转发 tcp:38301 tcp:38301
当我尝试使用应用内客户端连接到服务器时,我遇到的错误如下:E/WSCLient:连接到/127.0.0.1(端口 38301(时出错:连接失败:ECONNDENY (连接被拒绝(
我还尝试使用本地主机而不是 127.0.0.1。
这是我的服务器的源代码:
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();
}
}
}
我不知道为什么我无法与我的服务器建立连接。希望你能帮上忙。
所以我通过改变我的实现方式解决了这个问题。我子类化了服务器和客户端类,然后它起作用了。:)