简体   繁体   English

将android客户端连接到sails.js

[英]connecting android client to sails.js

I'm using nkzawa socket.io android client . 我正在使用nkzawa socket.io android客户端 How can I connect it to sails.js server? 如何将其连接到sails.js服务器? I tried the approaches from Sending Socket request from Client (iOS & Android) to Sails.js Server and Simple Sails.js and Android Example but I get the following errors on my sails server: 我尝试了从客户端(iOS和Android)发送套接字请求到Sails.js服务器Simple Sails.js和Android示例的方法但我在我的sails服务器上遇到以下错误:

Running "watch" task
Waiting...
verbose: A socket is being allowed to connect, but the session could not be loaded.  Will create an empty, one-time session to use for the life of the socket connection.  Details:
 Error: Session could not be loaded
    at _createError (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/lib/hooks/session/index.js:271:21)
    at Immediate._onImmediate (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/lib/hooks/session/index.js:274:13)
    at processImmediate [as _immediateCallback] (timers.js:358:17) { [Error: Session could not be loaded] code: 'E_SESSION' }
verbose: A socket is being allowed to connect, but the session could not be loaded.  Will create an empty, one-time session to use for the life of the socket connection.  Details:
 Error: Session could not be loaded
    at _createError (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/lib/hooks/session/index.js:271:21)
    at Immediate._onImmediate (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/lib/hooks/session/index.js:274:13)
    at processImmediate [as _immediateCallback] (timers.js:358:17) { [Error: Session could not be loaded] code: 'E_SESSION' }
verbose: Could not fetch session, since connecting socket has no cookie (is this a cross-origin socket?)
Generated a one-time-use cookie:sails.sid=s%3AZfXLqUZt5kQ4M0hPd-NI6-0AzciPjXDf.IjhbWcteKvKB9h5v0kcxYRis8Lo1JhpTHN9eTweXpdwand saved it on the socket handshake.
This will start this socket off with an empty session, i.e. (req.session === {})
That "anonymous" section will only last until the socket is disconnected unless you persist the session id in your database,
or by setting the set-cookie response header for an HTTP request that you *know* came from the same user (etc)
Alternatively, just make sure the socket sends a `cookie` header or query param when it initially connects.
verbose: Could not fetch session, since connecting socket has no cookie (is this a cross-origin socket?)
Generated a one-time-use cookie:sails.sid=s%3AFOsga3uhvU0N-CZsgAPAjY_IWZlGb5Cr.%2FjoQX1LkUU0OWeB1Kt3pr7cbFFOkMAirJ5ODPLwXgNAand saved it on the socket handshake.
This will start this socket off with an empty session, i.e. (req.session === {})
That "anonymous" section will only last until the socket is disconnected unless you persist the session id in your database,
or by setting the set-cookie response header for an HTTP request that you *know* came from the same user (etc)
Alternatively, just make sure the socket sends a `cookie` header or query param when it initially connects.
verbose: Receiving incoming message from Socket.io:  [ { url: '/user/new' } ]
error: Error (SAILS:HOOK:SOCKETS:PARSE_VIRTUAL_REQ):: Failed to parse incoming socket.io request.
    at new constructor (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/standalone/create-error-constructor.js:38:16)
    at Errorpack.factory [as PARSE_VIRTUAL_REQ] (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/standalone/create-error-factory.js:34:12)
    at respondWithParseError (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/lib/receive-incoming-sails-io-msg.js:247:29)
    at receiveIncomingSailsIOMsg (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/lib/receive-incoming-sails-io-msg.js:41:14)
    at Socket.<anonymous> (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/lib/on-connect.js:84:9)
    at Socket.emit (events.js:107:17)
    at Socket.onevent (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/lib/socket.js:330:8)
    at Socket.onpacket (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/lib/socket.js:290:12)
    at Client.ondecoded (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/lib/client.js:193:14)
    at Decoder.Emitter.emit (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20)
    at Decoder.add (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/node_modules/socket.io-parser/index.js:247:12)
    at Client.ondata (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/lib/client.js:175:18)
    at Socket.emit (events.js:107:17)
    at Socket.onPacket (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/node_modules/engine.io/lib/socket.js:99:14)
    at WebSocket.emit (events.js:129:20)
    at WebSocket.Transport.onPacket (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/node_modules/engine.io/lib/transport.js:91:8) { [Error (SAILS:HOOK:SOCKETS:PARSE_VIRTUAL_REQ):: Failed to parse incoming socket.io request.]
  code: 'SAILS:HOOK:SOCKETS:PARSE_VIRTUAL_REQ',
  name: 'Error (SAILS:HOOK:SOCKETS:PARSE_VIRTUAL_REQ):',
  status: 400,
  message: 'Failed to parse incoming socket.io request.',
  stack: 'Error (SAILS:HOOK:SOCKETS:PARSE_VIRTUAL_REQ):: Failed to parse incoming socket.io request.\n    at new constructor (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/standalone/create-error-constructor.js:38:16)\n    at Errorpack.factory [as PARSE_VIRTUAL_REQ] (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/standalone/create-error-factory.js:34:12)\n    at respondWithParseError (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/lib/receive-incoming-sails-io-msg.js:247:29)\n    at receiveIncomingSailsIOMsg (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/lib/receive-incoming-sails-io-msg.js:41:14)\n    at Socket.<anonymous> (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/lib/on-connect.js:84:9)\n    at Socket.emit (events.js:107:17)\n    at Socket.onevent (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/lib/socket.js:330:8)\n    at Socket.onpacket (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/lib/socket.js:290:12)\n    at Client.ondecoded (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/lib/client.js:193:14)\n    at Decoder.Emitter.emit (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20)\n    at Decoder.add (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/node_modules/socket.io-parser/index.js:247:12)\n    at Client.ondata (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/lib/client.js:175:18)\n    at Socket.emit (events.js:107:17)\n    at Socket.onPacket (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/node_modules/engine.io/lib/socket.js:99:14)\n    at WebSocket.emit (events.js:129:20)\n    at WebSocket.Transport.onPacket (/usr/local/node-v0.12.4-linux-x64/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/node_modules/engine.io/lib/transport.js:91:8)',
  details: 'No url provided in request: [object Object]' }

This is my android client: 这是我的android客户端:

JSONArray arr = new JSONArray();
JSONObject obj = new JSONObject();
try {
    obj.put("url", "/user/new");
} catch (JSONException e) {
    e.printStackTrace();
}
arr.put(obj);
socket.emit("get", arr);

In the object that you have constructed to emit to the server, skipping the JSONArray wrapper should fix the issue. 在您构造为要发送到服务器的对象中,跳过JSONArray包装器应该可以解决问题。 Change the code to this: 将代码更改为:

JSONObject obj = new JSONObject();
try {
    obj.put("url", "/user/new");
} catch (JSONException e) {
    e.printStackTrace();
}
socket.emit("get", obj);

If the problem still persists, there is another area that can be looked into. 如果问题仍然存在,还有另一个可以研究的领域。

Socket-io will face issue of parsing virtual request if client id is lower than 0.11.0 . 如果客户端ID低于0.11.0 Socket-io将面临解析虚拟请求的问题。 A fix for this is editing the parse-sdk-metadata.js file which can be found in /node_modules/sails-hook-sockets/lib/ or /node_modules/sails/node_modules/sails-hook-sockets/lib/ depending on your NPM version. 解决这个问题的方法是编辑parse-sdk-metadata.js文件,该文件可以在/node_modules/sails-hook-sockets/lib/ /node_modules/sails/node_modules/sails-hook-sockets/lib/具体取决于你的NPM版本。

In it, change the following : 在其中,更改以下内容:

if (!(handshake.headers[SDK_META_PARAMS.version] || handshake.query[SDK_META_PARAMS.version])) {
            handshake.query[SDK_META_PARAMS.version] = '0.9.0';
}

TO: 至:

if (!(handshake.headers[SDK_META_PARAMS.version] || handshake.query[SDK_META_PARAMS.version])) {
            handshake.query[SDK_META_PARAMS.version] = '0.11.0';
}

This should solve the Error (SAILS:HOOK:SOCKETS:PARSE_VIRTUAL_REQ) that you receive! 这应该解决您收到的Error (SAILS:HOOK:SOCKETS:PARSE_VIRTUAL_REQ)

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

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