蒸汽 - 创建Web套接字

时间:2016-11-14 08:16:38

标签: swift vapor

我正在尝试使用Vapor创建一个小型聊天应用程序,但我遇到了问题。我在ios上使用Socket.IO并且我总是找不到错误页面。

你能指导我找出我的错误吗?

蒸汽代码:

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()

}

1 个答案:

答案 0 :(得分:2)

您似乎正在使用wss中的socketURL

仅当您通过TLS(SSL)托管应用程序时才会这样做。

尝试仅使用ws或通过TLS托管应用程序。