[英]SocketIO Android not emitting connection event
I'm using Android native socketio library and NodeJS. 我正在使用Android本机socketio库和NodeJS 。
// on my Android activity
private Socket mSocket;
{
try {
mSocket = IO.socket("http://IP_OF_MY_SERVER");
} catch (URISyntaxException e) {}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSocket.connect();
}
. 。
// on my nodejs app
var restify = require('restify');
var server = restify.createServer({
name: 'myapp',
version: '1.0.0'
});
var io = require('socket.io')(server.server);
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
io.on('connection', function(socket){
console.log('a user connected');
socket.on('new-item', function(data) {
console.log(data);
});
});
I'm following official guides but still mSocket.connect()
not emitting connection
event and there is no a user connected
on my console when I launch activity. 我遵循官方指南,但是
mSocket.connect()
不会发出connection
事件,并且在启动活动时控制台上没有a user connected
。
Can you please tell me what is I'm missing and how can I debug it? 您能告诉我我缺少什么,如何调试它?
EDIT 编辑
NodeJS app on my Ubuntu server : http://paste.ubuntu.com/14233470/ 我的Ubuntu服务器上的NodeJS应用程序: http : //paste.ubuntu.com/14233470/
Android activity : http://paste.ubuntu.com/14233482/ Android活动: http : //paste.ubuntu.com/14233482/
EDIT 2 编辑2
I've changed my code like suggested here : 我已经改变了我的代码一样建议在这里 :
var io = require('socket.io').listen(server);
And this time I'm getting this error when I launch the activity 这次我在启动活动时遇到此错误
myapp listening at http://104.131.99.145:3000 myapp在http://104.131.99.145:3000上收听
http.js:691
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:691:11)
at ServerResponse.format (/var/www/biditapi.erayalakese.com/node_modules/restify/lib/response.js:145:10)
at ServerResponse.send (/var/www/biditapi.erayalakese.com/node_modules/restify/lib/response.js:338:14)
at emitRouteError (/var/www/biditapi.erayalakese.com/node_modules/restify/lib/server.js:201:13)
at onRoute (/var/www/biditapi.erayalakese.com/node_modules/restify/lib/server.js:754:21)
at Router.find (/var/www/biditapi.erayalakese.com/node_modules/restify/lib/router.js:608:5)
at Server._route (/var/www/biditapi.erayalakese.com/node_modules/restify/lib/server.js:747:21)
at routeAndRun (/var/www/biditapi.erayalakese.com/node_modules/restify/lib/server.js:705:14)
at Server._handle (/var/www/biditapi.erayalakese.com/node_modules/restify/lib/server.js:725:9)
at Server.onRequest (/var/www/biditapi.erayalakese.com/node_modules/restify/lib/server.js:326:14)
at Server.EventEmitter.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2108:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
at Socket.socket.ondata (http.js:1966:22)
at TCP.onread (net.js:525:27)
A few key notes: 一些主要注意事项:
Create a Socket
object: 创建一个
Socket
对象:
private Socket mSocket; { try { mSocket = IO.socket(CHAT_SERVER_URL); // Your server's URL } catch (URISyntaxException e) { throw new RuntimeException(e); } }
onCreate()
: connect & set the socket error listener onCreate()
:连接并设置套接字错误侦听器
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError); mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError); mSocket.connect(); } private Emitter.Listener onConnectError = new Emitter.Listener() { @Override public void call(Object... args) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "Unable to connect to NodeJS server", Toast.LENGTH_LONG).show(); } }); } };
Disconnect the Socket in the onDestroy()
method 在
onDestroy()
方法中断开套接字
@Override protected void onDestroy() { super.onDestroy(); mSocket.disconnect(); mSocket.off(Socket.EVENT_CONNECT_ERROR, onConnectError); mSocket.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError); }
Not getting the error toast is the first milestone to setting this up right. 没有得到错误的消息是正确设置的第一个里程碑。
(Open the command line with admin privileges, run these commands:)
(使用管理员权限打开命令行,运行以下命令:)
netsh wlan set hostednetwork mode=allow ssid=NodeSocket key=1234okok
netsh wlan设置托管网络模式=允许ssid = NodeSocket密钥= 1234okok
netsh wlan start hostednetwork
netsh wlan启动托管网络
Make sure that your Android device is connected to this network, & that your Node.js
server is up & running. 确保您的Android设备已连接到该网络,并且您的
Node.js
服务器已启动并正在运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.