【问题标题】:Vapor - Create web socketVapor - 创建网络套接字
【发布时间】:2016-11-14 08:16:37
【问题描述】:

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

}

【问题讨论】:

  • 我不确定 socket-io 是不是一个通用的 websocket 库。我相信它也打算与后端的专有套接字库进行通信。您可能想尝试使用红蜘蛛或其他东西与 Vapor 进行交流。
  • 如果下面的答案有效,请将此问题标记为已解决
  • 我遇到了一个问题,它不能与 SocketIOClient 一起使用。我试过红蜘蛛,它奏效了。

标签: swift vapor


【解决方案1】:

您似乎在 socketURL 中使用了 wss

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多