Skip to content

Commit

Permalink
Improve protocols, implement delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaelIsaev committed Dec 22, 2018
1 parent 0cfddd1 commit 5de8a19
Show file tree
Hide file tree
Showing 21 changed files with 597 additions and 310 deletions.
26 changes: 26 additions & 0 deletions Sources/WS/Channel.swift
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
}
}
39 changes: 39 additions & 0 deletions Sources/WS/Event.swift
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
}
}
43 changes: 43 additions & 0 deletions Sources/WS/EventIdentifier.swift
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") }
}
10 changes: 0 additions & 10 deletions Sources/WS/NotificationMessage.swift

This file was deleted.

57 changes: 57 additions & 0 deletions Sources/WS/WS+Broadcast.swift
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))
}
}
31 changes: 31 additions & 0 deletions Sources/WS/WS+Channels.swift
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)
}
}
}
52 changes: 0 additions & 52 deletions Sources/WS/WS+Client.swift

This file was deleted.

33 changes: 0 additions & 33 deletions Sources/WS/WS+Stuff.swift

This file was deleted.

13 changes: 0 additions & 13 deletions Sources/WS/WS+Subscription.swift

This file was deleted.

Loading

0 comments on commit 5de8a19

Please sign in to comment.