我正在测试actioncable和nativescript并得到上面的错误。我已经在我的github上设置了测试应用程序。

一个用于ActionCableNativescript

本机脚本应用程序来自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

本机脚本控制台:
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

最佳答案

我遇到了同样的错误,并通过用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});

我希望这能给您一个想法,如果您需要更多帮助,请告诉我。

10-08 07:49