-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve protocols, implement delegate
- Loading branch information
1 parent
0cfddd1
commit 5de8a19
Showing
21 changed files
with
597 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// Channel.swift | ||
// WS | ||
// | ||
// Created by Mihael Isaev on 22/12/2018. | ||
// | ||
|
||
import Foundation | ||
|
||
public class Channel { | ||
public let uid: String | ||
public var clients: [WSClient] = [] | ||
init(_ uid: String) { | ||
self.uid = uid | ||
} | ||
} | ||
|
||
extension Channel: Hashable { | ||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(uid) | ||
} | ||
|
||
public static func == (lhs: Channel, rhs: Channel) -> Bool { | ||
return lhs.uid == rhs.uid | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// Event.swift | ||
// WS | ||
// | ||
// Created by Mihael Isaev on 22/12/2018. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct NoPayload: Codable {} | ||
|
||
public struct Event<P: Codable>: Codable { | ||
public let event: String | ||
public let payload: P? | ||
public init (event: String, payload: P? = nil) { | ||
self.event = event | ||
self.payload = payload | ||
} | ||
} | ||
|
||
public protocol EventProtocol: Codable { | ||
associatedtype P: Codable | ||
|
||
var event: EventIdentifier<P> { get } | ||
var payload: P? { get } | ||
} | ||
|
||
public struct EventPrototype: Codable { | ||
public var event: String | ||
} | ||
|
||
public struct OutgoingEvent<P: Codable>: Codable { | ||
var event: String | ||
var payload: P? | ||
init(_ event: String, payload: P?) { | ||
self.event = event | ||
self.payload = payload | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// EventIdentifier.swift | ||
// WS | ||
// | ||
// Created by Mihael Isaev on 22/12/2018. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct EventIdentifier<P: Codable>: Equatable, Hashable, CustomStringConvertible, ExpressibleByStringLiteral, Codable { | ||
/// The unique id. | ||
public let uid: String | ||
|
||
/// See `CustomStringConvertible`. | ||
public var description: String { | ||
return uid | ||
} | ||
|
||
/// Create a new `EventIdentifier`. | ||
public init(_ uid: String) { | ||
self.uid = uid | ||
} | ||
|
||
/// See `ExpressibleByStringLiteral`. | ||
public init(stringLiteral value: String) { | ||
self.init(value) | ||
} | ||
} | ||
|
||
//MARK: Local protocol events | ||
|
||
public struct JoinPayload: Codable { | ||
public var channel: UUID | ||
} | ||
|
||
public struct LeavePayload: Codable { | ||
public var channel: UUID | ||
} | ||
|
||
extension EventIdentifier { | ||
public static var join: EventIdentifier<JoinPayload> { return .init("join") } | ||
public static var leave: EventIdentifier<LeavePayload> { return .init("leave") } | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// WS+Broadcast.swift | ||
// WS | ||
// | ||
// Created by Mihael Isaev on 22/12/2018. | ||
// | ||
|
||
import Foundation | ||
import Vapor | ||
|
||
extension WS { | ||
public func broadcast(on: Worker, _ clients: Set<WSClient>, _ text: String) throws -> Future<Void> { | ||
return clients.map { $0.emit(text) }.flatten(on: on) | ||
} | ||
|
||
public func broadcast(on: Worker, _ clients: Set<WSClient>, _ binary: Data) throws -> Future<Void> { | ||
return clients.map { $0.emit(binary) }.flatten(on: on) | ||
} | ||
|
||
func broadcast<T: Codable>(on: Worker, _ clients: Set<WSClient>, _ event: OutgoingEvent<T>) throws -> Future<Void> { | ||
let jsonData = try JSONEncoder().encode(event) | ||
guard let jsonString = String(data: jsonData, encoding: .utf8) else { | ||
throw WSError(reason: "Unable to preapare JSON string for broadcast message") | ||
} | ||
return clients.map { $0.emit(jsonString) }.flatten(on: on) | ||
} | ||
|
||
//MARK: Text | ||
|
||
public func broadcast(on: Worker, _ text: String) throws -> Future<Void> { | ||
return try broadcast(on: on, clients, text) | ||
} | ||
|
||
public func broadcast(on: Worker, to channel: String, _ text: String) throws -> Future<Void> { | ||
return try broadcast(on: on, clients.filter { $0.channels.contains(channel) }, text) | ||
} | ||
|
||
//MARK: Binary | ||
|
||
public func broadcast(on: Worker, _ binary: Data) throws -> Future<Void> { | ||
return try broadcast(on: on, clients, binary) | ||
} | ||
|
||
public func broadcast(on: Worker, to channel: String, _ binary: Data) throws -> Future<Void> { | ||
return try broadcast(on: on, clients.filter { $0.channels.contains(channel) }, binary) | ||
} | ||
|
||
//MARK: JSON | ||
|
||
public func broadcast<T: Codable>(on: Worker, _ event: EventIdentifier<T>, payload: T? = nil) throws -> Future<Void> { | ||
return try broadcast(on: on, clients, OutgoingEvent(event.uid, payload: payload)) | ||
} | ||
|
||
public func broadcast<T: Codable>(on: Worker, to channel: String, _ event: EventIdentifier<T>, payload: T? = nil) throws -> Future<Void> { | ||
return try broadcast(on: on, clients.filter { $0.channels.contains(channel) }, OutgoingEvent(event.uid, payload: payload)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// WS+Channels.swift | ||
// WS | ||
// | ||
// Created by Mihael Isaev on 22/12/2018. | ||
// | ||
|
||
import Foundation | ||
|
||
extension WS { | ||
func decodeEvent<P: Codable>(by identifier: EventIdentifier<P>, with data: Data) throws -> Event<P> { | ||
return try JSONDecoder().decode(Event<P>.self, from: data) | ||
} | ||
|
||
func joining(_ client: WSClient, data: Data) { | ||
if let event = try? decodeEvent(by: .join, with: data) { | ||
let uid = String(describing: event.payload?.channel) | ||
let channel = channels.first { $0.uid == uid } ?? channels.insert(Channel(uid)).memberAfterInsert | ||
channel.clients.append(client) | ||
print("join event to channel: " + uid) | ||
} | ||
} | ||
|
||
func leaving(_ client: WSClient, data: Data) { | ||
if let event = try? decodeEvent(by: .leave, with: data) { | ||
let uid = String(describing: event.payload?.channel) | ||
channels.first { $0.uid == uid }?.clients.removeAll { $0.cid == client.cid } | ||
print("leave event channel: " + uid) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.