0
我正在嘗試使用Vapor創建一個小型聊天應用程序,但我被卡住了。我在ios上使用Socket.IO,並且總是收到找不到的錯誤頁面。Vapor - 創建網絡套接字
你能指點我指出我的錯誤嗎?
蒸氣代碼:
drop.socket("ws") { req, ws in
print("New WebSocket connected: \(ws)")
// ping the socket to keep it open
try background {
while ws.state == .open {
try? ws.ping()
drop.console.wait(seconds: 10) // every 10 seconds
}
}
ws.onText = { ws, text in
print("Text received: \(text)")
// reverse the characters and send back
let rev = String(text.characters.reversed())
try ws.send(rev)
}
ws.onClose = { ws, code, reason, clean in
print("Closed.")
}
}
drop.run()
在客戶端:
func receiveMSGFromServer(){
let u = URL(string: "ws://localhost:8080/ws")
//"http://localhost:8080/"
let socket = SocketIOClient(socketURL: URL(string: "wss://localhost:8080/ws")!, config: [.log(true), .forcePolling(true)]) //SocketIOClient(socketURL: u!)
socket.emit("ws", ":emptyParam")
socket.on("ws") {data, ack in
print("Message for you! \(data[0])")
// ack("I got your message, and I'll send my response")
socket.emit("response", "Hello!")
}
socket.connect()
}
我不確定socket-io是一個通用的websocket庫。我相信它的目的是在後端與專有的套接字庫進行通信。你可能想嘗試用Starscream或與Vapor溝通的東西。 – Logan
如果回答下面的工作,標記此問題已解決 – Quver
我有一個問題,它不與SocketIOClient工作。我嘗試了Starscream,它工作。 –