如何修复“websocket 断开连接:无效的 HTTP 升级"错误使用红蜘蛛 [英] How to fix "websocket is disconnected: Invalid HTTP upgrade" error using Starscream

查看:146
本文介绍了如何修复“websocket 断开连接:无效的 HTTP 升级"错误使用红蜘蛛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 MAMP 和红蜘蛛库将 iPhone 连接到在我的 Macbook 上运行的网络服务器 https://github.com/daltoniam/Starscream使用 Cocoapods 安装并使用此 URL 设置 Web 服务器:localhost:8888但是我很头疼,因为代码不起作用......我遵循了所有在线指南,官方文档等,但没有一个对我有帮助......每当我运行代码时,控制台都会显示此消息:websocket 已断开连接:HTTP 升级无效

I want to connect an iPhone to a web server running on my Macbook using MAMP and Starscream Library https://github.com/daltoniam/Starscream Installed using Cocoapods and set up the web server with this URL:localhost:8888 But I'm having a lot of headaches because the code doesn't work... I followed all the guides online, the official documentation and so on but none of them helped me... Whenever I run the code, the console shows this message: websocket is disconnected: Invalid HTTP upgrade

所以也许是我的服务器坏了?我尝试改用 ws://echo.websocket.org/并且成功了!!!所以我想......因为它终于向我展示了:websocket is connected但同时也不是!!!事实上,socket.isConnected 方法给了我错误...之后,如果我调用 socket.disconnect() 它不会也可以工作,没有消息显示.

So maybe it's my server that's broken? I tried using instead ws://echo.websocket.org/ and it worked!!! So I thought... Because it finally showed me: websocket is connected But at the same time it wasn't!!! In fact the method socket.isConnected gave me false... After that if I called socket.disconnect() it doesn't work either, no messages show up.

import UIKit
import Starscream

class ViewController: UIViewController, WebSocketDelegate {

    let socket = WebSocket(url: URL(string:"ws://echo.websocket.org/")!)//websocket is connected
    /*
    let socket = WebSocket(url: URL(string: "ws://localhost:8888/")!) //websocket is disconnected: Invalid HTTP upgrade
    */
    override func viewDidLoad() {
        super.viewDidLoad()

        socket.delegate = self
        socket.connect()

        if socket.isConnected {
            print("hi1") //it doesn't get printed
        }
        if !socket.isConnected {
            print("hi2") //it gets printed
        }

    }

    func websocketDidConnect(socket: WebSocketClient) {
        print("websocket is connected")
    }

    func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
        if let e = error as? WSError {
            print("websocket is disconnected: \(e.message)")
        } else if let e = error {
            print("websocket is disconnected: \(e.localizedDescription)")
        } else {
            print("websocket disconnected")
        }
    }

    func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
        print("Received text: \(text)")
    }

    func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
        print("Received data: \(data.count)")
    }

}

推荐答案

所以我终于明白了这个问题:Apache 默认不支持 Web Sockets...这就是我收到错误消息的原因.然后代码与ws://echo.websocket.org/"一起工作.你只需要把所有的 socket.x 函数放在 viewDidLoad() 之外就可以让它们工作,例如在断开连接按钮中的 socket.disconnect,在发送按钮中的 socket.write 等等.

So I finally understood the problem: Apache doesn't support Web Sockets by default... that's why I got the error message. Then the code works with "ws://echo.websocket.org/". You just have to put all socket.x functions outside viewDidLoad() to make them work, for example socket.disconnect in a Disconnect Button, socket.write in a sendButton etc.

例如:

let socket = WebSocket(url: URL(string: "ws://echo.websocket.org/")!) 

override func viewDidLoad() {
    super.viewDidLoad()

    socket.delegate = self
    socket.connect()

}

@IBAction func sendButtonPressed(_ sender: UIButton) {
    socket.write(string: textField.text!)
}

这篇关于如何修复“websocket 断开连接:无效的 HTTP 升级"错误使用红蜘蛛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆