[0.1.0] recipients provided by client
This commit is contained in:
parent
f16ad0d08e
commit
21e19f51b0
|
@ -59,10 +59,11 @@ Every function in this protocol is optional. It means `SLClient` provide default
|
|||
```swift
|
||||
extension SLClient {
|
||||
static func receivedData(_ message: Data, from: WebSocketConnection) -> Bool { return false }
|
||||
static func sendMessage(_ message: SLMessage, from client: String) { }
|
||||
static func sendMessage(_ message: SLMessage, from client: String) -> [String] { return message.recipients }
|
||||
static func statusMessage(_ command: SLMessageCommand, from client: String) -> [String]? { return nil }
|
||||
}
|
||||
```
|
||||
Sending message via `sendMessage` protocol method. Provide `SLMessage` object which hold recipients parsed from socket message. This can be chat room ids or simple client ids to which message should be delivered.
|
||||
|
||||
#### SLService
|
||||
|
||||
|
|
|
@ -11,12 +11,12 @@ import KituraWebSocket
|
|||
|
||||
public protocol SLClient {
|
||||
static func receivedData(_ message: Data, from: WebSocketConnection) -> Bool
|
||||
static func sendMessage(_ message: SLMessage, from client: String)
|
||||
static func sendMessage(_ message: SLMessage, from client: String) -> [String]
|
||||
static func statusMessage(_ command: SLMessageCommand, from client: String) -> [String]?
|
||||
}
|
||||
|
||||
extension SLClient {
|
||||
public static func receivedData(_ message: Data, from: WebSocketConnection) -> Bool { return false }
|
||||
public static func sendMessage(_ message: SLMessage, from client: String) { }
|
||||
public static func sendMessage(_ message: SLMessage, from client: String) -> [String] { return message.recipients ?? [] }
|
||||
public static func statusMessage(_ command: SLMessageCommand, from client: String) -> [String]? { return nil }
|
||||
}
|
||||
|
|
|
@ -79,15 +79,13 @@ public class SLService<T: SLClient>: WebSocketService {
|
|||
}
|
||||
|
||||
private func broadcast(from client: String, message: SLMessage) {
|
||||
guard let recipients = message.recipients else { return }
|
||||
if message.command == .base64Message || message.command == .textMessage {
|
||||
let recipients = T.sendMessage(message, from: client)
|
||||
connection.exertions({ connections in
|
||||
for (_, (client: clienId, connection: connection)) in connections where recipients.contains(clienId) {
|
||||
connection.send(message: message.make(client))
|
||||
}
|
||||
})
|
||||
|
||||
if message.command == .base64Message || message.command == .textMessage {
|
||||
T.sendMessage(message, from: client)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue