forked from mrniko/netty-socketio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleRoomBroadcastOperations.java
More file actions
123 lines (109 loc) · 4.02 KB
/
SingleRoomBroadcastOperations.java
File metadata and controls
123 lines (109 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Copyright (c) 2012-2019 Nikita Koksharov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.corundumstudio.socketio;
import com.corundumstudio.socketio.misc.IterableCollection;
import com.corundumstudio.socketio.protocol.Packet;
import com.corundumstudio.socketio.protocol.PacketType;
import com.corundumstudio.socketio.store.StoreFactory;
import com.corundumstudio.socketio.store.pubsub.DispatchMessage;
import com.corundumstudio.socketio.store.pubsub.PubSubType;
import java.util.Arrays;
import java.util.Collection;
/**
* @Author: liangjiaqi
* @Date: 2020/8/8 6:08 PM
*/
public class SingleRoomBroadcastOperations implements BroadcastOperations {
private final String namespace;
private final String room;
private final Iterable<SocketIOClient> clients;
private final StoreFactory storeFactory;
public SingleRoomBroadcastOperations(String namespace, String room, Iterable<SocketIOClient> clients, StoreFactory storeFactory) {
super();
this.namespace = namespace;
this.room = room;
this.clients = clients;
this.storeFactory = storeFactory;
}
private void dispatch(Packet packet) {
this.storeFactory.pubSubStore().publish(
PubSubType.DISPATCH,
new DispatchMessage(this.room, packet, this.namespace));
}
@Override
public Collection<SocketIOClient> getClients() {
return new IterableCollection<SocketIOClient>(clients);
}
@Override
public void send(Packet packet) {
for (SocketIOClient client : clients) {
client.send(packet);
}
dispatch(packet);
}
@Override
public <T> void send(Packet packet, BroadcastAckCallback<T> ackCallback) {
for (SocketIOClient client : clients) {
client.send(packet, ackCallback.createClientCallback(client));
}
ackCallback.loopFinished();
}
@Override
public void disconnect() {
for (SocketIOClient client : clients) {
client.disconnect();
}
}
@Override
public void sendEvent(String name, SocketIOClient excludedClient, Object... data) {
Packet packet = new Packet(PacketType.MESSAGE);
packet.setSubType(PacketType.EVENT);
packet.setName(name);
packet.setData(Arrays.asList(data));
for (SocketIOClient client : clients) {
if (client.getSessionId().equals(excludedClient.getSessionId())) {
continue;
}
client.send(packet);
}
dispatch(packet);
}
@Override
public void sendEvent(String name, Object... data) {
Packet packet = new Packet(PacketType.MESSAGE);
packet.setSubType(PacketType.EVENT);
packet.setName(name);
packet.setData(Arrays.asList(data));
send(packet);
}
@Override
public <T> void sendEvent(String name, Object data, BroadcastAckCallback<T> ackCallback) {
for (SocketIOClient client : clients) {
client.sendEvent(name, ackCallback.createClientCallback(client), data);
}
ackCallback.loopFinished();
}
@Override
public <T> void sendEvent(String name, Object data, SocketIOClient excludedClient, BroadcastAckCallback<T> ackCallback) {
for (SocketIOClient client : clients) {
if (client.getSessionId().equals(excludedClient.getSessionId())) {
continue;
}
client.sendEvent(name, ackCallback.createClientCallback(client), data);
}
ackCallback.loopFinished();
}
}