@@ -1,64 +1,6 @@
1import Vapor
1import Vapor
4− private(set) static var sockets = SafeDict<Int, WebSocket>()
6− static func insert(_ socket: WebSocket) -> Int {
13− static func remove(at id: Int) {
18− private static var id = 0
22−let enc: JSONEncoder = {
23− var out = JSONEncoder()
27/// Called after your application has initialized.
3/// Called after your application has initialized.
28public func boot(_ app: Application) throws {
4public func boot(_ app: Application) throws {
29− let akakiy = User(name: User.Name(first: "Акакий", second: "Программистович"))
5+ // your code here
30− User.storage[akakiy.id] = akakiy
32− app.eventLoop.scheduleRepeatedTask(initialDelay: .seconds(10), delay: .seconds(10)) { (task: RepeatedTask) in
33− let _ = try app.client().get("https://baconipsum.com/api/?type=all-meat¶s=1&start-with-lorem=0")
34− .flatMap { try $0.content.decode([String].self) }
36− guard let text = texts.first else { return }
38− let p = Post.init(text: text, user: akakiy.id)
40− Post.storage[p.id] = p
42− let event = PostEvent.added(p.id)
43− let t = PostEvent.events.append(event)
45− OpenSockets.sockets.values.forEach { (ws: WebSocket) in
46− ws.send(try! enc.encode(EventResponse(update: event, token: t)))
51− let _ = app.eventLoop.scheduleRepeatedTask(initialDelay: .seconds(205), delay: .seconds(10)) { (task: RepeatedTask) throws -> Void in
52− if (Post.storage.count > 20) {
53− if let key = Post.storage.removeRandom() {
54− let event = PostEvent.deleted(key)
55− let t = PostEvent.events.append(event)
56− OpenSockets.sockets.values.forEach { (ws: WebSocket) in
57− ws.send(try! enc.encode(EventResponse(update: event, token: t)))