简体   繁体   English

连接Android时socket.io向服务器发送一些数据

[英]socket.io send some data to server when connect android

in javascript and node.js i can send data to server when connect like this 在javascript和node.js中,我可以像这样连接时将数据发送到服务器

var s = io('http://216.157.91.131:8080/', { query: "foo=bar" });
s.connect();

var c = io.connect('http://216.157.91.131:8080/', { query: "foo=bar" });

and in server i can get the data like this 在服务器上我可以得到这样的数据

var io = require('socket.io')(server);

io.use(function(socket, next) {
  var handshakeData = socket.request;
  console.log("middleware:", handshakeData._query['foo']);

});

now in android how can i do same thing ? 现在在android中我该怎么做? iam use IAM使用

com.github.nkzawa:socket.io-client:0.4.2 com.github.nkzawa:socket.io客户端:0.4.2

for android and its code like this 对于Android及其代码

    IO.socket(host);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }  
    client.on("id", messageHandler.onId);

    client.connect();

now if i add some data with connection like javscript its show to me error 现在,如果我添加一些与javscript之类的连接数据,向我显示错误

          JSONObject id = new JSONObject();
          id.put("user_id", 51);
          IO.socket(host,id);

error message its 错误消息

cannot reslove method 'socket(java.lang.string,org.json.JSONObject)' 无法重新使用方法'socket(java.lang.string,org.json.JSONObject)'

i found sloution 我发现失窃

 IO.Options opts = new IO.Options();

opts.query = "auth_token=" + yourDataValue;
Socket socket = IO.socket("http://localhost", opts);

and u can get it by 你可以得到它

var io = require('socket.io')(server);

io.use(function(socket, next) {
  var handshakeData = socket.request;

  var data = handshakeData._query['auth_token'];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM