我已经设法编译了一个 Socket.IO/Android 项目,现在我正试图让它与 NodeJS 服务器上的 Socket.IO 通信。我将项目放在我的 Drop Box 上:
https://dl.dropboxusercontent.com/u/86164338/socketIOAndroid.zip
一切都编译并运行,但我没有看到 NodeJS 的输出。
这是基于: https ://github.com/Gottox/socket.io-java-client
请注意,有关于编译 Java 代码并将 jar 文件复制到 libs 文件夹的说明:
git clone git://github.com/Gottox/socket.io-java-client.git
cd socket.io-java-client
ant jar mv jar/socketio.jar
/path/to/your/libs/project
任何帮助是极大的赞赏。
Java 代码:
package com.depictlabs.testsockets03;
import java.net.MalformedURLException;
import java.net.URI;
import org.json.JSONObject;
import io.socket.SocketIO;
import io.socket.IOCallback;
import io.socket.IOAcknowledge;
import io.socket.SocketIOException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SocketIO socket;
try{
System.out.println("Initializing Connection.");
socket = new SocketIO("http://localhost:3000/");
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");
}
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
System.out.println("Server triggered event '" + event + "'");
}
}
);
try {
// This line is cached until the connection is established.
System.out.println("Sending message to server.");
socket.send("user message");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
节点代码:
var http = require('http')
, io = require('socket.io');
var app = http.createServer();
app.listen(3000);
console.log('Server running at http://127.0.0.1:3000/');
// Socket.IO server
var io = io.listen(app)
, nicknames = {};
io.sockets.on('connection', function (socket) {
socket.on('user message', function (msg) {
console.log('Server running at http://127.0.0.1:3000/');
socket.broadcast.emit('user message', {user: socket.nickname, message: msg.message});
});
socket.on('nickname', function (nick, fn) {
console.log('Server running at http://127.0.0.1:3000/');
nickname = nick.nickname;
if (nicknames[nickname]) {
fn(true);
} else {
fn(false);
nicknames[nickname] = socket.nickname = nickname;
socket.broadcast.emit('announcement', {user: nickname, action: 'connected'});
io.sockets.emit('nicknames', nicknames);
}
});
socket.on('disconnect', function () {
console.log('Server running at http://127.0.0.1:3000/');
if (!socket.nickname) return;
delete nicknames[socket.nickname];
socket.broadcast.emit('announcement', {user: socket.nickname, action: 'disconected'});
socket.broadcast.emit('nicknames', nicknames);
});
});