【问题标题】:Getting error code: 1006 when running nativescript-actioncable with ios使用 ios 运行 nativescript-actioncable 时出现错误代码:1006
【发布时间】:2017-04-06 23:33:16
【问题描述】:

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

ActionCableNativescript 一个

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

【问题讨论】:

  • 有同样的错误

标签: ios ruby-on-rails ruby-on-rails-5 nativescript actioncable


【解决方案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});

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

【讨论】:

    【解决方案2】:

    错误1006表示客户端部分由于某些错误而终止,因此可能与插件有关。也许您可以验证该插件是否可以正常工作(例如,使用另一个服务 this 对其进行测试)?

    【讨论】:

    猜你喜欢
    • 2017-08-27
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多