swift - Vapor - 创建网络套接字

标签 swift vapor

我正在尝试使用 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()

最佳答案

看起来您正在 socketURL 中使用 wss

这仅在您通过 TLS (SSL) 托管应用程序时才有效。

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

关于swift - Vapor - 创建网络套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40584379/

相关文章:

php - 如何从 PHP 获取回显到 Swift UIAlertController

swift - 如何使用 vapor 3.x 构建 vapor 工具箱

swift - 设置 Vapor 和 MongoKitten

linux - 随机字母数字字符串 Linux Swift 3

mysql - Vapor 3 流利的 MySQL : save on model adds 10 to auto_increment

ios - 核心数据 : Correct way to update within a NSUrlRequest

ios - Facebook Swift SDK : loginManager Type of expression is ambiguous error

ios - EAAccessory.Name 与配对的设备名称不匹配

xcode - 此应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏

vapor - 让根指向 Vapor 中的静态网页