我正在使用" swifter"作为本地HTTP服务器。我正在使用"星际奶油"进行Websocket。
不幸的是,当我尝试连接时获取websocket is disconnected: Optional("The operation couldn’t be completed. (Starscream.WSError error 1.)"))
。
我尝试从浏览器访问Localhost,看起来HTTP服务器正常工作。
以下是我正在处理的代码:
simpleserver:
import Foundation
import Swifter
class SimpleServer{
let server: HttpServer = HttpServer()
public func startServer(){
do {
server["/hello"] = { .ok(.htmlBody("You asked for bhuvesh ($0)")) }
try server.start(8000)
}
catch{
print("Server could not be started")
}
}
}
ViewController:
import UIKit
import Starscream
class ViewController: UIViewController, WebSocketDelegate {
@IBOutlet weak var button: UIButton!
var socket: WebSocketClient!
var simpleServer = SimpleServer()
override func viewDidLoad() {
super.viewDidLoad()
simpleServer.startServer()
socket = WebSocket(url: URL(string: "ws://localhost:8000")!)
socket.delegate = self
socket.connect()
}
func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
print("websocket is disconnected: (error?.localizedDescription))")
}
func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
print("got some text: (text)")
}
func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
print("got some data: (data.count)")
}
func websocketDidConnect(socket: WebSocketClient) {
print("websocket is connected")
}
}
我的问题完全相同。您是否尝试过使用Mac和Webocat连接它?如果它在那里失败了两个,那么您的服务器可能与我遇到的问题可能与我完全相同。