2012-07-09 164 views
0

编辑: 我想通过Web Interface在Android和浏览器上实现测验应用程序。 我正在寻找一种在服务器和客户端之间进行通信的方式。我试过socket.io,但无法使用android。使用Android和浏览器客户端的节点服务器

我在nodeter(nodester.com)上使用了一个node.js服务器。 我试过一些库,但无法让它工作。

我现在einaros/WS工作从https://github.com/einaros/ws

服务器代码:

var clients = [], 
numClients = 0; 


var WebSocketServer = require('ws').Server, 
    wss = new WebSocketServer({port: 20083}); 

wss.on('connection', function(ws) { 

    ws.on('message', function(message) { 
    console.log(wss.clients); 
    console.log('received: %s', message); 
    incomingMessage(message, ws) 
    }); 

    /* 
    ws.on('eigenesEvent', function(message) { 
    console.log('eigenes Event ausgelöst: ' + message); 
    }); 
    */ 
}); 

function incomingMessage(msg, ws) { 
    //console.log(wss.clients); 
    var obj = JSON.parse(msg); 

    if(obj.type == "connect") { 

     for(var i=0;i<clients.length;i++) { 
     if(clients[i] == obj.id) { 
      ws.send(JSON.stringify({ 
      to: obj.id, 
      message: "name vergeben" 
      })); 
      return; 
     } 
     } 
     clients[numClients] = obj.id; 
     numClients++; 
     for(var i=0;i<clients.length;i++) { 
     console.log("Client" + i + ": " + clients[i]); 
     } 
     ws.send(JSON.stringify({ 
      to: "all", 
      message: obj.id + " connected" 
     })); 
    } 

    if(obj.type == "disconnect") { 

    for(var i=0;i<clients.length;i++) { 
     if(clients[i] == obj.id) { 
     clients.splice(i, 1); 
     numClients--; 
     for(var i=0;i<clients.length;i++) { 
      console.log("Client" + i + ": " + clients[i]); 
     } 
     } 
    } 

    ws.send(JSON.stringify({ 
     to: "all", 
     message: obj.id + " disconnected" 
    })); 
    return; 
    } 

    if(obj.type == "answer") { 
    if("id" in obj) { 
     if(obj.answer == "a") { 
     ws.send(JSON.stringify({ 
      to: obj.id, 
      message: "a is correct" 
     })); 
     } else { 
     ws.send(JSON.stringify({ 
      to: obj.id, 
      message: "answer is incorrect" 
     })); 
     } 
    } 
    } 

    if(obj.type == "something") { 
    if("id" in obj) { 
     ws.send(JSON.stringify({ 
     to: obj.id, 
     message: "received: " + obj.message 
     })); 
    } 
    } 
} 



从HTML的网站,我可以通过连接到服务器:

connect = function() { 
    var host = "ws://einaros.nodester.com"; 

     try{ 
      socket = new WebSocket(host); 
      console.log('WebSocket - status ' + socket.readyState); 

      socket.onopen = function(msg) { 
       console.log("Welcome - status " + this.readyState); 
       socket.send(JSON.stringify({ 
        id: model.getClientName(), 
        type: "connect" 
       })); 
       model.setConnectionStatus(true); 
      }; 

      socket.onmessage = function(msg) { 
       console.log("onmessage - msg: " + msg.data); 
       checkMessage(msg.data); 
      }; 

      socket.onclose = function(msg) { 
       console.log("Disconnected - status " + this.readyState); 
       model.setConnectionStatus(false); 
      }; 

     } 
     catch(ex){ 
      console.log(ex); 
     } 
}, 



On the An我使用AutobahnAndroid从机器人 - 客户端:http://autobahn.ws/android
为Android的客户端代码:

package ps.mediengestaltung; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import de.tavendo.autobahn.WebSocketConnection; 
import de.tavendo.autobahn.WebSocketException; 
import de.tavendo.autobahn.WebSocketHandler; 


public class MainActivity extends Activity { 



public final WebSocketConnection mConnection = new WebSocketConnection(); 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 


    final String wsuri = "ws://einaros.nodester.com"; 

    try { 
     mConnection.connect(wsuri, new WebSocketHandler() { 

      @Override 
      public void onOpen() { 
       Log.d("TAG", "Status: Connected to " + wsuri); 
       mConnection.sendTextMessage("Hello Server!"); 
      } 

      @Override 
      public void onTextMessage(String payload) { 
       Log.d("TAG", "Got echo: " + payload); 
      } 

      @Override 
      public void onClose(int code, String reason) { 
       Log.d("TAG", "Connection lost."); 
      } 
     }); 
    } catch (WebSocketException e) { 
     Log.d("TAG", e.toString()); 
    } 

} 

} 


在logcat中,我得到:
8月8日至1日:48:13.017:d/TAG (704):状态:连接到ws://einaros.nodester.com
08-01 08:48:13.167:D/TAG(704):连接丢失。


我在做什么错?任何提示?

+0

OT;网络套接字在Android中工作良好吗?除了上述课程中的错误之外。:) – ninetwozero 2012-07-09 14:31:05

+0

这是我的第一个android上的websockets项目,所以我不能说任何关于兼容性的东西 – freakimkaefig 2012-07-10 10:18:03

+0

关于AutobahnAndroid代码:你需要设置wsuri指向运行node.js的机器 - 这是_not_ localhost。本地主机是你的Android手机。 我建议你首先确保你有一个使用ws/einaros的工作服务器,你可以成功地从Chrome连接。 只有继续与Android的东西.. – oberstet 2012-07-12 08:35:37

回答

2

原因可能是:Weberknecht只实现WebSocket的(过时的)Hixie-76版本。

您可能会尝试AutobahnAndroid,它实现了WebSocket的最终RFC6455版本。

另一件事:你正在使用的WebSocket服务器不再维护(据我所知)。它也只实现了Hixie-76 - Chrome/Firefox不再支持。

尝试其中之一:

免责声明:我是高速公路的作家和工作Tavendo。

+0

我更新了我的问题,你可以看看吗? – freakimkaefig 2012-07-11 17:18:52

+0

我终于得到它与einaros/ws和autobahnandroid合作。非常感谢你的帮助。 – freakimkaefig 2012-08-02 08:42:10

1

你正在问你的手机连接到本地主机。你没有在电话上运行节点吗? :)

URI url = new URI("ws://127.0.0.1:8080/test"); 

这应该改为指向您的节点地址/端口。

+0

对不起,但我用真正的网址“ws://websocketserver.nodester.com:19572” 我只复制了weberknecht网站的代码。 – freakimkaefig 2012-07-09 14:53:11

相关问题