Skip to content

Commit

Permalink
update package and web socket
Browse files Browse the repository at this point in the history
  • Loading branch information
saroar committed Dec 17, 2023
1 parent e5dacbd commit 66b3bee
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 97 deletions.
39 changes: 24 additions & 15 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"location" : "https://github.com/AddaMeSPB/AddaSharedModels.git",
"state" : {
"branch" : "CleanUpBackEndModel",
"revision" : "b8d3d7af1b7baf18c89329f01d933c378820b22c"
"revision" : "6b3a0d5b7a5c5918ad4eee5aaa1e826fe5e6c0c8"
}
},
{
Expand Down Expand Up @@ -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"
}
},
{
Expand Down Expand Up @@ -203,17 +203,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/RediStack.git",
"state" : {
"revision" : "30a43b019569625d2d9972c4fe89e06dbdc09e64",
"version" : "1.5.1"
"revision" : "a476f69cbbe31de5ba040309630d365d13c687f8",
"version" : "1.6.1"
}
},
{
"identity" : "routing-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/routing-kit.git",
"state" : {
"revision" : "88077f2c9d12777dcc89562fa581888ff7ba14ae",
"version" : "4.8.1"
"revision" : "17a7a3facce8285fd257aa7c72d5e480351e7698",
"version" : "4.8.2"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand Down Expand Up @@ -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"
}
},
{
Expand Down Expand Up @@ -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",
Expand All @@ -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"
}
},
{
Expand Down
117 changes: 56 additions & 61 deletions Sources/App/Webbsockets/ChatHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}
}
Expand Down
19 changes: 0 additions & 19 deletions Sources/App/configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void, Error>) in
Self.runQueue.async { [self] in
do {
try run()
continuation.resume()
} catch {
continuation.resume(throwing: error)
}
}
}
}
}
18 changes: 16 additions & 2 deletions Sources/App/routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 66b3bee

Please sign in to comment.