Node.js y Websocket en Android Eclipse
Gracias por investigar mi pregunta.
Soy un principiante en el desarrollo de Android y actualmente estoy tratando de hacer una aplicación de chat que use socket y node js.
Usé una biblioteca de github, es decirhttps://github.com/Gottox/socket.io-java-client y pude enviar un mensaje a mi servidor y pude recibir un mensaje de regreso también en "new_client_message" que se muestra en la imagen de logcat a continuación.
El problema al que me enfrento es que no puedo encontrar la manera de obtener los mensajes del servidor para mostrarlos en una vista de lista. Sería genial si alguien pudiera ayudarme. Gracias por adelantado
Mi LogCat:
Código Node.js:
var socket = require('socket.io');
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = socket.listen(server);
var users = {};
var clients = {};
io.sockets.on( 'connection', function(socket) {
console.log("New client !");
var socket_id = socket.id;
var hs = socket.handshake;
//store user with socket id
if(hs.query.user_id !== undefined){
users[hs.query.user_id] = socket.id; // connected user with its socket.id
}
clients[socket.id] = socket; // add the client data to the hash
socket.on('disconnect', function () {
delete clients[socket.id]; // remove the client from the array
delete users[hs.query.username]; // remove connected user & socket.id
});
socket
.on('new_message', function(data){
clients[users['admin']].emit('new_message', {'original_data':data,'socket_id': socket_id } );
})
.on('new_client_message', function(data){
console.log(data);
clients[data.socket_id].emit('new_client_message', data.message);
})
;
});
server.listen(3030);
Y aquí está mi código:
socket = null;
try {
socket = new SocketIO("http://xxx.xxx.xx.xx:3030/");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
socket.connect(new IOCallback() {
@Override
public void onMessage(JSONObject json, IOAcknowledge ack) {
try {
System.out.println("Server said:" + json.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(String data, IOAcknowledge ack) {
System.out.println("Server said: " + data);
}
@Override
public void onError(SocketIOException socketIOException) {
System.out.println("an Error occured");
socketIOException.printStackTrace();
}
@Override
public void onDisconnect() {
System.out.println("Connection terminated.");
}
@Override
public void onConnect() {
System.out.println("Connection established");
}
//here I was trying to get the message from the server, convert into string and
//display in list view(thats when the error displayed)
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
System.out.println("Server triggered event '" + event + "'");
System.out.println("senrver ack" + ack);
System.out.println("server said this " + args[0]);
rohan = args[0].toString();
mListData.add(rohan);
//mListData.add(args[0].toString());
}
});
//mListData.add(rohan);
//sending the message from the app when onclick to the server which is sucessful
send.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// This line is cached until the connection is establisched.
//socket.send("new_message");
chat = messag.getText().toString();
karan = client_email+";"+chat;
// This line is cached until the connection is establisched.
socket.emit("new_message", karan);
click();
}
private void click() {
// TODO Auto-generated method stub
//getting the username
name = getIntent().getExtras().getString("name");
String output = name.substring(0,1).toUpperCase() + name.substring(1);
// converting the TextView message to string
msg = messag.getText().toString();
// combining the username and the text view
messag.setText(output +" " + ":" + " " + msg);
//showing the final combination in the listview
mListData.addAll(messag.getText().toString());
messag.setText("");
//when the list view is updated with data, then it goes to that position
lvList.setSelection(mListData.getCount() -1);
post();
}
Indíqueme dónde debo agregar más código para mostrar el mensaje del servidor node.js en la vista de lista. Estoy seguro de que estoy cometiendo algún error y agradecería si alguien pudiera corregirme. Muchas gracias !