我正在使用nkzawa socket.io android client。 如何将其连接到sails.js服务器? 我尝试了Sending Socket request from Client (iOS & Android) to Sails.js Server和Simple Sails.js and Android Example的方法,但我的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);
答案 0 :(得分:0)
在您构造为要向服务器发出的对象中,跳过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)
!