简单Sails.js和Android例 [英] Simple Sails.js and Android Example
问题描述
我真的打开使用sails.io和Android的套接字连接挣扎。我想实现的那一刻简直是打印socketid在sails.js服务器的控制台。
I am really struggling with opening a socket connection using sails.io and android. What I am trying to achieve at the moment is simply to print the socketid in the console of the sails.js server.
Android的阵营:
Android Side:
我使用nkzwa的socket.io.client库
(编译'com.github.nkzawa:socket.io客户端:0.4.2')
I am using nkzwa's socket.io.client library ( compile 'com.github.nkzawa:socket.io-client:0.4.2')
这是code,我使用android系统中的AsyncTask的内部:
This is the code that I am using in android inside an AsyncTask:
private Socket mSocket;
{
try {
mSocket = IO.socket("http://192.168.0.80:3000/batches/");
} catch (URISyntaxException e) {}
}
@Override
protected Void doInBackground(Void... params) {
mSocket.connect();
mSocket.emit("getSocketID");
}
和我batchescontroller是这样的:
and my batchescontroller looks like this:
module.exports = {
getSocketID: function(req, res) {
if (!req.isSocket) return res.badRequest();
var socketId = sails.sockets.id(req.socket);
// => "BetX2G-2889Bg22xi-jy"
console.log(socketId)
return res.ok('My socket ID is: ' + socketId);
}
}
在运行我以为我会得到我的船帆情况下输出的控制台日志的任务。
When running the task I thought that I would get the console log outputted in my sails instance.
有人能看到我在做什么错了?
Can anyone see what I am doing wrong?
推荐答案
我得到了它的工作是这样的:
I got it working like this:
private Socket mSocket;
{
try {
mSocket = IO.socket("http://192.168.0.80:3000");
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
JSONObject obj1 = new JSONObject();
try {
obj1.put("url","/batches/getSocketID");
} catch (JSONException e) {
e.printStackTrace();
}
mSocket.emit("get",obj1);
mSocket.connect();
这篇关于简单Sails.js和Android例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!