我想創建一個node.js和android之間的基本聊天應用程序,當我運行服務器和客戶端時,我可以在日誌中看到他們連接和我的android應用程序發送消息但服務器永遠不會收到它,我andoid代碼:Socket.io android node.js
private Socket socket;
{
IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.reconnection = true;
try {
socket = IO.socket("http://192.168.3.7:100");
} catch (URISyntaxException e) {
Log.e("abc", "index=" + e);
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_promociones);
socket.on("new message", onNewMessage);
socket.connect();
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
ObjWifi.startScan();
messages =String.valueOf(id)+';'+ Arrays.toString(ret)+';'+String.valueOf(zona);
socket.emit(messages);
String str;
int i1 = r.nextInt(4 - 1) + 1;
if (zona == 0) {
str = "img_" + 4;
} else {
str = "img_" + zona + "_" + i1;
}
fondo.setImageDrawable(getResources().getDrawable(getResourceID(str, "drawable", getApplicationContext())));
}
}
此外,我想使傳入消息(百達數)被存儲在一個稱爲透明變量。
我的服務器端代碼:
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var messages = [{
text: "Hola soy un mensaje",
author: "Daniel"
}];
app.use(express.static('public'));
io.on('connection', function(socket) {
var currentdate = new Date();
var datetime = currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
console.log('Alguien se ha conectado con Sockets',datetime);
socket.emit('messages', messages);
socket.on('disconnect',function(){
console.log('Alguien se ha desconectado',datetime);
});
socket.on('new-message', function(data) {
messages.push(data);
io.sockets.emit('messages', messages);
});
});
server.listen(100, function() {
console.log("Servidor corriendo en http://localhost:100");
});
使壓痕,becaouse它是很難讀 –