編輯: 我想通過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):連接丟失。
我在做什麼錯?任何提示?
OT;網絡套接字在Android中工作良好嗎?除了上述課程中的錯誤之外。:) – ninetwozero 2012-07-09 14:31:05
這是我的第一個android上的websockets項目,所以我不能說任何關於兼容性的東西 – freakimkaefig 2012-07-10 10:18:03
關於AutobahnAndroid代碼:你需要設置wsuri指向運行node.js的機器 - 這是_not_ localhost。本地主機是你的Android手機。 我建議你首先確保你有一個使用ws/einaros的工作服務器,你可以成功地從Chrome連接。 只有繼續與Android的東西.. – oberstet 2012-07-12 08:35:37