我正在测试actioncable和nativescript并得到上面的错误。我已经在我的github上设置了测试应用程序。
nativescript应用程序来自nativescript-actioncable包。这只发生在iOS上,Android连接正常。
我找不到关于这个问题的任何信息,这让我发疯了。任何帮助都会很棒。
以下是控制台输出:
Rails控制台:
Started GET "/cable" for 127.0.0.1 at 2017-04-06 16:24:50 -0700
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-04-06 16:24:50 -0700
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2017-04-06 16:24:50 -0700
Nativescript控制台:
CONSOLE LOG file:///app/tns_modules/nativescript-actioncable/lib/action_cable.js:46:33: [ActionCable] ConnectionMonitor reopening 1491521019978
CONSOLE LOG file:///app/tns_modules/nativescript-actioncable/lib/action_cable.js:46:33: [ActionCable] Reopening WebSocket, current state is closed 1491521019978
CONSOLE LOG file:///app/tns_modules/nativescript-actioncable/lib/action_cable.js:46:33: [ActionCable] Opening WebSocket, current state is closed, subprotocols: actioncable-v1-json,actioncable-unsupported 1491521019978
答案 0 :(得分:1)
我遇到了同样的错误,并通过将actioncable插件替换为nativescript-websockets并编写代码与我自己与actioncable服务器进行通信来解决此问题。
以下是我使用的示例代码
this.socket = new WebSocket('ws://192.168.8.104:3000/cable', []);
this.socket.on('open', function(evt) {
console.log("Hey I'm open");
var identifier = JSON.stringify({"channel": "ConversationsChannel"});
var data = {"command": "subscribe", "identifier": identifier};
var payload = JSON.stringify(data);
evt.target.send(payload);
});
this.socket.on('message', function(socket, message) {console.log("Got a message", message);});
this.socket.on('close', function(socket, code, reason) { console.log("Socket was closed because: ", reason, " code: ", code); });
this.socket.on('error', function(socket, error) { console.log("Socket had an error", error);});
并发送消息
public sendMessage() {
// "{"command":"message","identifier":"{\"channel\":\"ConversationsChannel\",\"conversation_id\":\"12\"}","data":"{\"message\":\"ssss\",\"conversation_id\":\"12\",\"user_id\":39,\"action\":\"send_message\"}"}"
var data = JSON.stringify({"message": "ssss","conversation_id":"12","user_id":39,"action":"send_message"});
var identifier = JSON.stringify({"channel": "ConversationsChannel"});
var payload = JSON.stringify({"command": "message", "identifier": identifier, "data": data});
this.socket.send(payload);
}
同样,您可以编写发送有效负载的取消订阅
"{"command":"unsubscribe","identifier":"{\"channel\":\"ConversationsChannel\",\"conversation_id\":\"12\"}"}"
或者您可以转到node_modules / nativescript-actioncable / lib / action_cable.js并从此处更改第240行
this.webSocket = new WS(this.consumer.url, {protocols: protocols, headers: headers});
到这个
this.webSocket = new WS(this.consumer.url, {protocols: ["actioncable-v1-json"], headers: headers});
我希望这会给你一个想法,如果你需要更多的帮助,请告诉我。
答案 1 :(得分:0)
错误1006表示客户端部分由于某些错误而终止,因此可能是与插件相关的问题。也许您可以验证插件是否正常工作(例如,使用其他服务this进行测试)?