6

我正在使用nkzawa socket.io android 客户端。如何将它连接到sails.js 服务器?我尝试了从客户端(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]' }

这是我的安卓客户端:

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);
4

1 回答 1

0

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

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

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

如果客户端 id 低于 .Socket-io 将面临解析虚拟请求的问题0.11.0。解决此问题的方法是编辑parse-sdk-metadata.js可以在 NPM 版本中找到/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)您收到的问题!

于 2018-02-01T11:03:00.367 回答