以下是一个Android客户端连接Node.js服务器的示例代码:
private Socket mSocket;
{
try {
mSocket = IO.socket("http://your_server_ip:port");
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
以下是Node.js服务器启动Socket.io的示例代码:
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var port = 3000;
app.listen(port, function () {
console.log('Server listening at port %d', port);
});
io.on('connection', function (socket) {
console.log('New client connected');
socket.on('message', function (data) {
console.log('Received message: ' + data);
socket.emit('reply', 'Hello, client!');
});
});
注意,上述示例仅供参考。具体实现可能因客户端和服务器的特定环境而有所不同。