将 Socket 请求从客户端(iOS 和 Android)发送到 Sails.js 服务器 [英] Sending Socket request from Client (iOS & Android) to Sails.js Server

查看:24
本文介绍了将 Socket 请求从客户端(iOS 和 Android)发送到 Sails.js 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 socket.io 与 iOS 和 Android 应用程序一起使用,但这里存在一些问题.我在问是否有人真正有解决方案.

I'm trying to use socket.io with iOS and Android App, but there is some problem here. I'm asking if there is anyone who actually has solutions.

如何从客户端(iOS、Android)发送 socket.io 请求,我认为 iOS 和 Android 有 socket.io 库,iOS 库有 sendEvent/Message/JSON 方法.但是,我找不到在帆上获取事件的方法.https://github.com/pkyeck/socket.IO-objc https://github.com/koush/AndroidAsync那么有什么方法可以从客户端发送 socket.io 事件?所以我可以像

How can I send socket.io request from client (iOS, Android), I think there is socket.io libraries for iOS and Android and iOS lib has sendEvent/Message/JSON methods. However, I couldn't find a way to get the events at the sails. https://github.com/pkyeck/socket.IO-objc https://github.com/koush/AndroidAsync So is there any way that I can send socket.io event from client? So I can use it like

join: function (req, res) {
    sails.sockets.join(req.socket, req.param('room'));
    return res.ok();
  },

有什么办法可以知道当前的套接字连接是来自客户端(ios/android)还是来自 Web 前端?

Is there any way that I can know whether current socket connection is from client (ios/android) or web front end?

onConnect: function(session, socket) {

    console.log(session);
    console.log(socket);

  },

当我将套接字与服务器连接时,我需要 url 来连接.我可以在网址中添加一些参数.有什么办法可以知道我用哪些参数来连接服务器?

when I connect socket with server, I need url to connect. I can add some params at the url. Is there any way that I can know which parameters I've used to connect with server?

谢谢.

推荐答案

从 iOS 到 Sails.js 服务器(使用 Socket.io)

From iOS to Sails.js Server (Using Socket.io)

SocketIO *socket;
NSString *url = [NSString stringWithFormat:@"/v1/controller/action?param1=%@&param2=%@", @"data1", @"data2"];
NSDictionary *params = @{@"url" : url};
[socket sendEvent:@"get" withData:data];

从 Android 到 Sails.js 服务器(使用 AndroidAsync,感谢 ArtworkAD)

From Android to Sails.js Server (Using AndroidAsync, thanks to ArtworkAD)

JSONArray arr = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("url", String.format("/v1/controller/action?param1=%s&param2=%s", "data1", "data2"));
arr.put(obj);
socketClient.emit("get", arr);

风帆控制器

action: function(req, res) {
    var data1 = req.param('param1');
    var data2 = req.param('param2');
    sails.log.debug(req.socket);
    return res.ok();
}

这篇关于将 Socket 请求从客户端(iOS 和 Android)发送到 Sails.js 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆