我的应用程序使用 socket.io,无法连接到 node.js 服务器。
服务器节点.js
var app = require('http').createServer()
var io = require('socket.io')(app);
app.listen(1000);
io.on('connection', function (client) {
client.name = client.remoteAddress + ':' + client.remotePort;
console.log(client.name + ' connected!');
client.on('sensorChanged', function (data) {
console.log("HERE");
console.log(data);
});
});
Run Code Online (Sandbox Code Playgroud)
安卓应用:
SocketIO socket = new SocketIO();
try {
socket.connect("http://localhost:1000/", this);
txtView.setText("connected");
} catch (MalformedURLException e) {
e.printStackTrace();
}
socket.emit("sensorChanged", "argument1");
Run Code Online (Sandbox Code Playgroud)
当我连接到服务器时,服务器不会说“socket.name connected”,也不会发出事件“sensorChanged”。问题出在哪儿?
我在静态方法中读取 cookie 时遇到问题。我试过:
static void Method()
{
Page page = HttpContext.Current.Handler as Page;
HttpCookie reader= page.Request.Cookies["myCookie"];
}
Run Code Online (Sandbox Code Playgroud)
但我认为这不起作用。
你有什么想法我该怎么做吗?