From 66b3beed45832c1c244dc933c69d267398d8397e Mon Sep 17 00:00:00 2001 From: saroar Date: Sun, 17 Dec 2023 21:19:48 +0000 Subject: [PATCH] update package and web socket --- Package.resolved | 39 +++++--- Sources/App/Webbsockets/ChatHandle.swift | 117 +++++++++++------------ Sources/App/configure.swift | 19 ---- Sources/App/routes.swift | 18 +++- 4 files changed, 96 insertions(+), 97 deletions(-) diff --git a/Package.resolved b/Package.resolved index 3c3320a..33e2fb0 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,7 +6,7 @@ "location" : "https://github.com/AddaMeSPB/AddaSharedModels.git", "state" : { "branch" : "CleanUpBackEndModel", - "revision" : "b8d3d7af1b7baf18c89329f01d933c378820b22c" + "revision" : "6b3a0d5b7a5c5918ad4eee5aaa1e826fe5e6c0c8" } }, { @@ -59,8 +59,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/vapor/console-kit.git", "state" : { - "revision" : "ccd0773b3ad3c67a19918aaef6903678592bb087", - "version" : "4.9.0" + "revision" : "7d0898ed481e1855ec549924ab701bdc6f754b18", + "version" : "4.10.2" } }, { @@ -203,8 +203,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/swift-server/RediStack.git", "state" : { - "revision" : "30a43b019569625d2d9972c4fe89e06dbdc09e64", - "version" : "1.5.1" + "revision" : "a476f69cbbe31de5ba040309630d365d13c687f8", + "version" : "1.6.1" } }, { @@ -212,8 +212,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/vapor/routing-kit.git", "state" : { - "revision" : "88077f2c9d12777dcc89562fa581888ff7ba14ae", - "version" : "4.8.1" + "revision" : "17a7a3facce8285fd257aa7c72d5e480351e7698", + "version" : "4.8.2" } }, { @@ -230,8 +230,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-algorithms.git", "state" : { - "revision" : "bcd4f369ac962bc3e5244c9df778739f8f5bdbf1", - "version" : "1.1.0" + "revision" : "f6919dfc309e7f1b56224378b11e28bab5bccc42", + "version" : "1.2.0" } }, { @@ -248,8 +248,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/pointfreeco/swift-case-paths", "state" : { - "revision" : "5da6989aae464f324eef5c5b52bdb7974725ab81", - "version" : "1.0.0" + "revision" : "ed7facdd4a361514b46e3bbc6238cd41c84be4ec", + "version" : "1.1.1" } }, { @@ -302,8 +302,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-nio.git", "state" : { - "revision" : "853522d90871b4b63262843196685795b5008c46", - "version" : "2.61.1" + "revision" : "702cd7c56d5d44eeba73fdf83918339b26dc855c", + "version" : "2.62.0" } }, { @@ -360,6 +360,15 @@ "version" : "0.13.0" } }, + { + "identity" : "swift-syntax", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-syntax.git", + "state" : { + "revision" : "6ad4ea24b01559dde0773e3d091f1b9e36175036", + "version" : "509.0.2" + } + }, { "identity" : "swift-url-routing", "kind" : "remoteSourceControl", @@ -374,8 +383,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/vapor/vapor.git", "state" : { - "revision" : "3bf4e73955d029743c233f127f4259a101a764f5", - "version" : "4.85.0" + "revision" : "d682e05fdb64c9f7da01af096a73cd11bb7ab755", + "version" : "4.86.2" } }, { diff --git a/Sources/App/Webbsockets/ChatHandle.swift b/Sources/App/Webbsockets/ChatHandle.swift index 5660108..f7de50e 100644 --- a/Sources/App/Webbsockets/ChatHandle.swift +++ b/Sources/App/Webbsockets/ChatHandle.swift @@ -19,76 +19,71 @@ actor WebsocketHandle { } func connectionHandler(ws: WebSocket, req: Request) { - - ws.onPong { ws in - ws.onText { (ws, text) in - print(#line, text) - } - } - + // ws.onPing { ws in // ws.onText { (ws, text) in // print(#line, text) // } // } - - ws.onText { [self] ws, text in - guard let data = text.data(using: .utf8) else { - req.logger.error("Wrong encoding for received message for connect web socket") - return - } - - let string = String(data: data, encoding: .utf8) - req.logger.info("\(#function) \(#line) \(string as Any)") - - guard let chatOutGoingEvent = ChatOutGoingEvent.decode(data: data) else { - - req.logger.notice("unacceptableData for connect web socket") - Task { - try await ws.close(code: .unacceptableData) + req.eventLoop.execute { + ws.onText { [self] ws, text in + guard let data = text.data(using: .utf8) else { + req.logger.error("Wrong encoding for received message for connect web socket") + return } - return - } - - let user = req.payload.user - guard let userID = user.id else { - req.logger.error("Cant found user from req.payload") - return - } - - switch chatOutGoingEvent { - case .connect: - - Task { - await wsClients.join(id: userID, on: ws) + + let string = String(data: data, encoding: .utf8) + req.logger.info("\(#function) \(#line) \(string as Any)") + + guard let chatOutGoingEvent = ChatOutGoingEvent.decode(data: data) else { + + req.logger.notice("unacceptableData for connect web socket") + Task { + try await ws.close(code: .unacceptableData) + } + return } - req.logger.info("web socker connect for user \(user.email ?? user.fullName ?? "")") - - case .disconnect: - - Task { - await wsClients.leave(id: userID) + + let user = req.payload.user + guard let userID = user.id else { + req.logger.error("Cant found user from req.payload") + return } - req.logger.info("web socker remove for user \(user.email ?? user.fullName ?? "")") - - case .message(let msg): - - Task { - try await wsClients.send(msg: msg, req: req) + + switch chatOutGoingEvent { + case .connect: + + Task { + await wsClients.join(id: userID, on: ws) + } + req.logger.info("web socker connect for user \(user.email ?? user.fullName ?? "")") + + case .disconnect: + + Task { + await wsClients.leave(id: userID) + } + req.logger.info("web socker remove for user \(user.email ?? user.fullName ?? "")") + + case .message(let msg): + + Task { + try await wsClients.send(msg: msg, req: req) + } + + case .conversation(let lastMessage): + + Task { + try await wsClients.send(msg: lastMessage, req: req) + } + + req.logger.info("conversation conversation: \(lastMessage)") + + case .notice(let msg): + req.logger.info("error: \(msg)") + case .error(let error): + req.logger.error("error: \(error.localizedDescription)") } - - case .conversation(let lastMessage): - - Task { - try await wsClients.send(msg: lastMessage, req: req) - } - - req.logger.info("conversation conversation: \(lastMessage)") - - case .notice(let msg): - req.logger.info("error: \(msg)") - case .error(let error): - req.logger.error("error: \(error.localizedDescription)") } } } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 27bfd4d..a890014 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -103,22 +103,3 @@ public func configure(_ app: Application) async throws { app.mount(app.router, use: siteHandler) } - - -extension Application { - private static let runQueue = DispatchQueue(label: "Run") - - /// We need to avoid blocking the main thread, so spin this off to a separate queue - public func customRun() async throws { - try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in - Self.runQueue.async { [self] in - do { - try run() - continuation.resume() - } catch { - continuation.resume(throwing: error) - } - } - } - } -} diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index 82280c3..de957e8 100644 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -7,9 +7,23 @@ func routes(_ app: Application) throws { try await req.view.render("index") } +// try app.group("v1") { api in +// let chat = api.grouped("chat") +// let webSocketController = WebsocketHandle.init(wsClients: .init()) +// try chat.register(collection: WebsocketController(wsController: webSocketController) ) +// } + + // Grouping API version v1 try app.group("v1") { api in + // Grouping chat let chat = api.grouped("chat") - let webSocketController = WebsocketHandle.init(wsClients: .init()) - try chat.register(collection: WebsocketController(wsController: webSocketController) ) + + // Initialize WebsocketHandle + let wsClients = WebsocketClients() // Assuming this is your WebSocket clients manager + let webSocketHandle = WebsocketHandle(wsClients: wsClients) + + // Initialize and register WebsocketController + let webSocketController = WebsocketController(wsController: webSocketHandle) + try chat.register(collection: webSocketController) } }