我有一个Android应用程序,想使用socket.io将一些数据发送到node.js服务器。该应用程序将连接,然后断开连接,但不显示任何发出的消息。
我已经尝试过更改端口并仔细检查IP地址。
服务器代码:
const express = require('express');
const http = require('http');
const port = 3000;
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
app.get('/', (req, res) => {
res.send('Server is running on port ' + port);
});
io.on('connection', (socket) => {
console.log('New App Connected');
socket.on('join', (appType) => {
console.log(appType + " : has joined the server. ");
});
socket.on('messagedetection', (messageContent) => {
console.log(messageContent);
});
socket.on('disconnect', () => {
console.log('App disconnected')
});
});
server.listen(port, () => {
console.log('Node app is running on port ' + port);
});
Android代码:
Socket mSocket;
final String URL = "http://192.168.1.16:3000/";
try {
mSocket = IO.socket(URL);
} catch (URISyntaxException e) {
e.printStackTrace();
mSocket.connect();
mSocket.emit("join", "Table App");
mSocket.emit("messagedetection", message);
mSocket.disconnect();
我在控制台中看到的只是“新应用已连接”,然后在大约10秒钟后显示“应用已断开连接”