将 android 客户端连接到 sails.js

connecting android client to sails.js

我正在使用 nkzawa socket.io android client。 如何将它连接到 sails.js 服务器? 我尝试了 Sending Socket request from Client (iOS & Android) to Sails.js Server and 中的方法,但我的 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]' }

这是我的 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);

在您构建的发送到服务器的对象中,跳过 JSONArray 包装器应该可以解决问题。将代码更改为:

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

如果问题仍然存在,可以查看另一个区域。

如果客户端 ID 低于 0.11.0,Socket-io 将面临解析虚拟请求的问题。解决此问题的方法是编辑 parse-sdk-metadata.js 文件,该文件可以在 /node_modules/sails-hook-sockets/lib/ /node_modules/sails/node_modules/sails-hook-sockets/lib/ 中找到,具体取决于您的 NPM 版本。

在其中,更改以下内容:

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

至:

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

这应该可以解决您收到的 Error (SAILS:HOOK:SOCKETS:PARSE_VIRTUAL_REQ)