SlideShare a Scribd company logo
Creating a WhatsApp Clone - Part XV
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container =
new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(8192);
container.setMaxBinaryMessageBufferSize(8192);
return container;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry
registry) {
registry.addHandler(myHandler(), "/socket");
}
@Bean
AppSocket
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container =
new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(8192);
container.setMaxBinaryMessageBufferSize(8192);
return container;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry
registry) {
registry.addHandler(myHandler(), "/socket");
}
@Bean
AppSocket
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container =
new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(8192);
container.setMaxBinaryMessageBufferSize(8192);
return container;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry
registry) {
registry.addHandler(myHandler(), "/socket");
}
@Bean
public WebSocketHandler myHandler() {
return new AppSocket();
}
@Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler(Executors.
newSingleThreadScheduledExecutor());
AppSocket
container.setMaxTextMessageBufferSize(8192);
container.setMaxBinaryMessageBufferSize(8192);
return container;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry
registry) {
registry.addHandler(myHandler(), "/socket");
}
@Bean
public WebSocketHandler myHandler() {
return new AppSocket();
}
@Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler(Executors.
newSingleThreadScheduledExecutor());
}
}
AppSocket
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
public class AppSocket extends TextWebSocketHandler {
private static Logger log =
Logger.getLogger(AppSocket.class.getName());
private static final Map<String, List<WebSocketSession>> clients =
new HashMap<>();
@Autowired
private UserService users;
private static boolean sendToToken(String token, String json) {
List<WebSocketSession> ws = clients.get(token);
WebSocketSession currentSocket = null;
try {
if(ws != null) {
TextMessage t = new TextMessage(json);
List<WebSocketSession> removeQueue = null;
for(WebSocketSession w : ws) {
currentSocket = w;
AppSocket
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
public class AppSocket extends TextWebSocketHandler {
private static Logger log =
Logger.getLogger(AppSocket.class.getName());
private static final Map<String, List<WebSocketSession>> clients =
new HashMap<>();
@Autowired
private UserService users;
private static boolean sendToToken(String token, String json) {
List<WebSocketSession> ws = clients.get(token);
WebSocketSession currentSocket = null;
try {
if(ws != null) {
TextMessage t = new TextMessage(json);
List<WebSocketSession> removeQueue = null;
for(WebSocketSession w : ws) {
currentSocket = w;
AppSocket
public class AppSocket extends TextWebSocketHandler {
private static Logger log =
Logger.getLogger(AppSocket.class.getName());
private static final Map<String, List<WebSocketSession>> clients =
new HashMap<>();
@Autowired
private UserService users;
private static boolean sendToToken(String token, String json) {
List<WebSocketSession> ws = clients.get(token);
WebSocketSession currentSocket = null;
try {
if(ws != null) {
TextMessage t = new TextMessage(json);
List<WebSocketSession> removeQueue = null;
for(WebSocketSession w : ws) {
currentSocket = w;
if(currentSocket.isOpen()) {
w.sendMessage(t);
AppSocket
new HashMap<>();
@Autowired
private UserService users;
private static boolean sendToToken(String token, String json) {
List<WebSocketSession> ws = clients.get(token);
WebSocketSession currentSocket = null;
try {
if(ws != null) {
TextMessage t = new TextMessage(json);
List<WebSocketSession> removeQueue = null;
for(WebSocketSession w : ws) {
currentSocket = w;
if(currentSocket.isOpen()) {
w.sendMessage(t);
return true;
} else {
if(removeQueue == null) {
removeQueue = new ArrayList<>();
}
removeQueue.add(w);
AppSocket
new HashMap<>();
@Autowired
private UserService users;
private static boolean sendToToken(String token, String json) {
List<WebSocketSession> ws = clients.get(token);
WebSocketSession currentSocket = null;
try {
if(ws != null) {
TextMessage t = new TextMessage(json);
List<WebSocketSession> removeQueue = null;
for(WebSocketSession w : ws) {
currentSocket = w;
if(currentSocket.isOpen()) {
w.sendMessage(t);
return true;
} else {
if(removeQueue == null) {
removeQueue = new ArrayList<>();
}
removeQueue.add(w);
AppSocket
private static boolean sendToToken(String token, String json) {
List<WebSocketSession> ws = clients.get(token);
WebSocketSession currentSocket = null;
try {
if(ws != null) {
TextMessage t = new TextMessage(json);
List<WebSocketSession> removeQueue = null;
for(WebSocketSession w : ws) {
currentSocket = w;
if(currentSocket.isOpen()) {
w.sendMessage(t);
return true;
} else {
if(removeQueue == null) {
removeQueue = new ArrayList<>();
}
removeQueue.add(w);
}
}
if(removeQueue != null) {
for(WebSocketSession w : removeQueue) {
ws.remove(w);
AppSocket
private static boolean sendToToken(String token, String json) {
List<WebSocketSession> ws = clients.get(token);
WebSocketSession currentSocket = null;
try {
if(ws != null) {
TextMessage t = new TextMessage(json);
List<WebSocketSession> removeQueue = null;
for(WebSocketSession w : ws) {
currentSocket = w;
if(currentSocket.isOpen()) {
w.sendMessage(t);
return true;
} else {
if(removeQueue == null) {
removeQueue = new ArrayList<>();
}
removeQueue.add(w);
}
}
if(removeQueue != null) {
for(WebSocketSession w : removeQueue) {
ws.remove(w);
AppSocket
List<WebSocketSession> ws = clients.get(token);
WebSocketSession currentSocket = null;
try {
if(ws != null) {
TextMessage t = new TextMessage(json);
List<WebSocketSession> removeQueue = null;
for(WebSocketSession w : ws) {
currentSocket = w;
if(currentSocket.isOpen()) {
w.sendMessage(t);
return true;
} else {
if(removeQueue == null) {
removeQueue = new ArrayList<>();
}
removeQueue.add(w);
}
}
if(removeQueue != null) {
for(WebSocketSession w : removeQueue) {
ws.remove(w);
}
}
AppSocket
List<WebSocketSession> ws = clients.get(token);
WebSocketSession currentSocket = null;
try {
if(ws != null) {
TextMessage t = new TextMessage(json);
List<WebSocketSession> removeQueue = null;
for(WebSocketSession w : ws) {
currentSocket = w;
if(currentSocket.isOpen()) {
w.sendMessage(t);
return true;
} else {
if(removeQueue == null) {
removeQueue = new ArrayList<>();
}
removeQueue.add(w);
}
}
if(removeQueue != null) {
for(WebSocketSession w : removeQueue) {
ws.remove(w);
}
}
AppSocket
List<WebSocketSession> ws = clients.get(token);
WebSocketSession currentSocket = null;
try {
if(ws != null) {
TextMessage t = new TextMessage(json);
List<WebSocketSession> removeQueue = null;
for(WebSocketSession w : ws) {
currentSocket = w;
if(currentSocket.isOpen()) {
w.sendMessage(t);
return true;
} else {
if(removeQueue == null) {
removeQueue = new ArrayList<>();
}
removeQueue.add(w);
}
}
if(removeQueue != null) {
for(WebSocketSession w : removeQueue) {
ws.remove(w);
}
}
AppSocket
removeQueue = new ArrayList<>();
}
removeQueue.add(w);
}
}
if(removeQueue != null) {
for(WebSocketSession w : removeQueue) {
ws.remove(w);
}
}
} else {
log.warning("No WS connections for token: " + token);
}
} catch(IOException err) {
log.log(Level.SEVERE, "Exception during sending message", err);
if(currentSocket != null && ws != null) {
ws.remove(currentSocket);
if(ws.isEmpty()) {
clients.remove(token);
}
}
}
AppSocket
removeQueue.add(w);
}
}
if(removeQueue != null) {
for(WebSocketSession w : removeQueue) {
ws.remove(w);
}
}
} else {
log.warning("No WS connections for token: " + token);
}
} catch(IOException err) {
log.log(Level.SEVERE, "Exception during sending message", err);
if(currentSocket != null && ws != null) {
ws.remove(currentSocket);
if(ws.isEmpty()) {
clients.remove(token);
}
}
}
return false;
}
AppSocket
}
public static boolean sendMessage(String token, String json) {
return sendToToken(token, json);
}
@Override
protected void handleTextMessage(WebSocketSession session,
TextMessage message) throws Exception {
JsonParser parser = JsonParserFactory.getJsonParser();
Map<String, Object> m = parser.parseMap(message.getPayload());
String type = (String)m.get("t");
if(type != null) {
if(type.equals("init")) {
String token = (String)m.get("tok");
Number time = (Number)m.get("time");
List<WebSocketSession> l = clients.get(token);
if(l == null) {
l = new ArrayList<>();
clients.put(token, l);
}
l.add(session);
AppSocket
public static boolean sendMessage(String token, String json) {
return sendToToken(token, json);
}
@Override
protected void handleTextMessage(WebSocketSession session,
TextMessage message) throws Exception {
JsonParser parser = JsonParserFactory.getJsonParser();
Map<String, Object> m = parser.parseMap(message.getPayload());
String type = (String)m.get("t");
if(type != null) {
if(type.equals("init")) {
String token = (String)m.get("tok");
Number time = (Number)m.get("time");
List<WebSocketSession> l = clients.get(token);
if(l == null) {
l = new ArrayList<>();
clients.put(token, l);
}
l.add(session);
return;
}
} else {
AppSocket
@Override
protected void handleTextMessage(WebSocketSession session,
TextMessage message) throws Exception {
JsonParser parser = JsonParserFactory.getJsonParser();
Map<String, Object> m = parser.parseMap(message.getPayload());
String type = (String)m.get("t");
if(type != null) {
if(type.equals("init")) {
String token = (String)m.get("tok");
Number time = (Number)m.get("time");
List<WebSocketSession> l = clients.get(token);
if(l == null) {
l = new ArrayList<>();
clients.put(token, l);
}
l.add(session);
return;
}
} else {
String typing = (String)m.get("typing");
String sentTo = (String)m.get("sentTo");
if(typing != null) {
String authorId = (String)m.get("authorId");
AppSocket
@Override
protected void handleTextMessage(WebSocketSession session,
TextMessage message) throws Exception {
JsonParser parser = JsonParserFactory.getJsonParser();
Map<String, Object> m = parser.parseMap(message.getPayload());
String type = (String)m.get("t");
if(type != null) {
if(type.equals("init")) {
String token = (String)m.get("tok");
Number time = (Number)m.get("time");
List<WebSocketSession> l = clients.get(token);
if(l == null) {
l = new ArrayList<>();
clients.put(token, l);
}
l.add(session);
return;
}
} else {
String typing = (String)m.get("typing");
String sentTo = (String)m.get("sentTo");
if(typing != null) {
String authorId = (String)m.get("authorId");
AppSocket
l = new ArrayList<>();
clients.put(token, l);
}
l.add(session);
return;
}
} else {
String typing = (String)m.get("typing");
String sentTo = (String)m.get("sentTo");
if(typing != null) {
String authorId = (String)m.get("authorId");
users.userTyping(authorId, sentTo, true);
} else {
String id = (String)m.get("id");
if(id == null) {
users.sendMessage(sentTo, m);
} else {
users.sendJSONTo(sentTo, message.getPayload());
}
}
}
}
AppSocket
l = new ArrayList<>();
clients.put(token, l);
}
l.add(session);
return;
}
} else {
String typing = (String)m.get("typing");
String sentTo = (String)m.get("sentTo");
if(typing != null) {
String authorId = (String)m.get("authorId");
users.userTyping(authorId, sentTo, true);
} else {
String id = (String)m.get("id");
if(id == null) {
users.sendMessage(sentTo, m);
} else {
users.sendJSONTo(sentTo, message.getPayload());
}
}
}
}
AppSocket
@Override
public void handleTransportError(WebSocketSession wss,
Throwable thrwbl) throws Exception {
log.log(Level.SEVERE, "Error during transport", thrwbl);
}
@Override
public void afterConnectionClosed(WebSocketSession wss, CloseStatus cs)
throws Exception {
for(String s : clients.keySet()) {
List<WebSocketSession> wl = clients.get(s);
for(WebSocketSession w : wl) {
if(w == wss) {
wl.remove(w);
if(wl.isEmpty()) {
clients.remove(s);
}
return;
}
}
}
}
AppSocket
@Override
public void afterConnectionClosed(WebSocketSession wss, CloseStatus cs)
throws Exception {
for(String s : clients.keySet()) {
List<WebSocketSession> wl = clients.get(s);
for(WebSocketSession w : wl) {
if(w == wss) {
wl.remove(w);
if(wl.isEmpty()) {
clients.remove(s);
}
return;
}
}
}
}
@Override
public boolean supportsPartialMessages() {
return false;
}
}
AppSocket

More Related Content

Similar to Creating a Whatsapp Clone - Part XV.pdf

Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfCreating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdf
ShaiAlmog1
 
Qt & Webkit
Qt & WebkitQt & Webkit
Qt & Webkit
QT-day
 
Correcting Common Async/Await Mistakes in .NET
Correcting Common Async/Await Mistakes in .NETCorrecting Common Async/Await Mistakes in .NET
Correcting Common Async/Await Mistakes in .NET
Brandon Minnick, MBA
 
Spring Transaction
Spring TransactionSpring Transaction
Spring Transaction
Jannarong Wadthong
 
Building real-time apps with WebSockets
Building real-time apps with WebSocketsBuilding real-time apps with WebSockets
Building real-time apps with WebSockets
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 
WebSockets Jump Start
WebSockets Jump StartWebSockets Jump Start
WebSockets Jump Start
Haim Michael
 
Mobile Software Engineering Crash Course - C06 WindowsPhone
Mobile Software Engineering Crash Course - C06 WindowsPhoneMobile Software Engineering Crash Course - C06 WindowsPhone
Mobile Software Engineering Crash Course - C06 WindowsPhone
Mohammad Shaker
 
Wonderful csom sps barcelona
Wonderful csom sps barcelonaWonderful csom sps barcelona
Wonderful csom sps barcelona
Sonja Madsen
 
Correcting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await MistakesCorrecting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await Mistakes
Brandon Minnick, MBA
 
Websockets talk at Rubyconf Uruguay 2010
Websockets talk at Rubyconf Uruguay 2010Websockets talk at Rubyconf Uruguay 2010
Websockets talk at Rubyconf Uruguay 2010
Ismael Celis
 
Workshop: Async and Parallel in C#
Workshop: Async and Parallel in C#Workshop: Async and Parallel in C#
Workshop: Async and Parallel in C#
Rainer Stropek
 
XQuery Rocks
XQuery RocksXQuery Rocks
XQuery Rocks
William Candillon
 
java sockets
 java sockets java sockets
java sockets
Enam Ahmed Shahaz
 
JavaScript Lessons 2023
JavaScript Lessons 2023JavaScript Lessons 2023
JavaScript Lessons 2023
Laurence Svekis ✔
 
Orsiso
OrsisoOrsiso
Orsiso
e27
 
[JEEConf-2017] RxJava as a key component in mature Big Data product
[JEEConf-2017] RxJava as a key component in mature Big Data product[JEEConf-2017] RxJava as a key component in mature Big Data product
[JEEConf-2017] RxJava as a key component in mature Big Data product
Igor Lozynskyi
 
Mobile Web 5.0
Mobile Web 5.0Mobile Web 5.0
Mobile Web 5.0
Michael Galpin
 
Not your Grandma's XQuery
Not your Grandma's XQueryNot your Grandma's XQuery
Not your Grandma's XQuery
William Candillon
 
Note Use Java Write a web server that is capable of processing only.pdf
Note Use Java Write a web server that is capable of processing only.pdfNote Use Java Write a web server that is capable of processing only.pdf
Note Use Java Write a web server that is capable of processing only.pdf
fatoryoutlets
 
Starting Vert.x in Eclipse
Starting Vert.x in EclipseStarting Vert.x in Eclipse
Starting Vert.x in Eclipse
Hyeonseok Yang
 

Similar to Creating a Whatsapp Clone - Part XV.pdf (20)

Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfCreating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdf
 
Qt & Webkit
Qt & WebkitQt & Webkit
Qt & Webkit
 
Correcting Common Async/Await Mistakes in .NET
Correcting Common Async/Await Mistakes in .NETCorrecting Common Async/Await Mistakes in .NET
Correcting Common Async/Await Mistakes in .NET
 
Spring Transaction
Spring TransactionSpring Transaction
Spring Transaction
 
Building real-time apps with WebSockets
Building real-time apps with WebSocketsBuilding real-time apps with WebSockets
Building real-time apps with WebSockets
 
WebSockets Jump Start
WebSockets Jump StartWebSockets Jump Start
WebSockets Jump Start
 
Mobile Software Engineering Crash Course - C06 WindowsPhone
Mobile Software Engineering Crash Course - C06 WindowsPhoneMobile Software Engineering Crash Course - C06 WindowsPhone
Mobile Software Engineering Crash Course - C06 WindowsPhone
 
Wonderful csom sps barcelona
Wonderful csom sps barcelonaWonderful csom sps barcelona
Wonderful csom sps barcelona
 
Correcting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await MistakesCorrecting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await Mistakes
 
Websockets talk at Rubyconf Uruguay 2010
Websockets talk at Rubyconf Uruguay 2010Websockets talk at Rubyconf Uruguay 2010
Websockets talk at Rubyconf Uruguay 2010
 
Workshop: Async and Parallel in C#
Workshop: Async and Parallel in C#Workshop: Async and Parallel in C#
Workshop: Async and Parallel in C#
 
XQuery Rocks
XQuery RocksXQuery Rocks
XQuery Rocks
 
java sockets
 java sockets java sockets
java sockets
 
JavaScript Lessons 2023
JavaScript Lessons 2023JavaScript Lessons 2023
JavaScript Lessons 2023
 
Orsiso
OrsisoOrsiso
Orsiso
 
[JEEConf-2017] RxJava as a key component in mature Big Data product
[JEEConf-2017] RxJava as a key component in mature Big Data product[JEEConf-2017] RxJava as a key component in mature Big Data product
[JEEConf-2017] RxJava as a key component in mature Big Data product
 
Mobile Web 5.0
Mobile Web 5.0Mobile Web 5.0
Mobile Web 5.0
 
Not your Grandma's XQuery
Not your Grandma's XQueryNot your Grandma's XQuery
Not your Grandma's XQuery
 
Note Use Java Write a web server that is capable of processing only.pdf
Note Use Java Write a web server that is capable of processing only.pdfNote Use Java Write a web server that is capable of processing only.pdf
Note Use Java Write a web server that is capable of processing only.pdf
 
Starting Vert.x in Eclipse
Starting Vert.x in EclipseStarting Vert.x in Eclipse
Starting Vert.x in Eclipse
 

More from ShaiAlmog1

The Duck Teaches Learn to debug from the masters. Local to production- kill ...
The Duck Teaches  Learn to debug from the masters. Local to production- kill ...The Duck Teaches  Learn to debug from the masters. Local to production- kill ...
The Duck Teaches Learn to debug from the masters. Local to production- kill ...
ShaiAlmog1
 
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfcreate-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdf
ShaiAlmog1
 
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfcreate-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdf
ShaiAlmog1
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
ShaiAlmog1
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdf
ShaiAlmog1
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdf
ShaiAlmog1
 
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfcreate-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdf
ShaiAlmog1
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdf
ShaiAlmog1
 
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfcreate-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdf
ShaiAlmog1
 
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfcreate-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdf
ShaiAlmog1
 
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfcreate-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdf
ShaiAlmog1
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdf
ShaiAlmog1
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfCreating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfCreating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfCreating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfCreating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfCreating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part IX.pdf
Creating a Whatsapp Clone - Part IX.pdfCreating a Whatsapp Clone - Part IX.pdf
Creating a Whatsapp Clone - Part IX.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part VI.pdf
Creating a Whatsapp Clone - Part VI.pdfCreating a Whatsapp Clone - Part VI.pdf
Creating a Whatsapp Clone - Part VI.pdf
ShaiAlmog1
 

More from ShaiAlmog1 (20)

The Duck Teaches Learn to debug from the masters. Local to production- kill ...
The Duck Teaches  Learn to debug from the masters. Local to production- kill ...The Duck Teaches  Learn to debug from the masters. Local to production- kill ...
The Duck Teaches Learn to debug from the masters. Local to production- kill ...
 
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfcreate-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdf
 
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfcreate-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdf
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdf
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdf
 
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfcreate-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdf
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdf
 
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfcreate-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdf
 
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfcreate-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdf
 
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfcreate-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdf
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdf
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdf
 
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfCreating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdf
 
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfCreating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfCreating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfCreating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdf
 
Creating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfCreating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdf
 
Creating a Whatsapp Clone - Part IX.pdf
Creating a Whatsapp Clone - Part IX.pdfCreating a Whatsapp Clone - Part IX.pdf
Creating a Whatsapp Clone - Part IX.pdf
 
Creating a Whatsapp Clone - Part VI.pdf
Creating a Whatsapp Clone - Part VI.pdfCreating a Whatsapp Clone - Part VI.pdf
Creating a Whatsapp Clone - Part VI.pdf
 

Recently uploaded

みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
Pixlogix Infotech
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 

Recently uploaded (20)

みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 

Creating a Whatsapp Clone - Part XV.pdf

  • 1. Creating a WhatsApp Clone - Part XV
  • 2. @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Bean public ServletServerContainerFactoryBean createWebSocketContainer() { ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean(); container.setMaxTextMessageBufferSize(8192); container.setMaxBinaryMessageBufferSize(8192); return container; } @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myHandler(), "/socket"); } @Bean AppSocket
  • 3. @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Bean public ServletServerContainerFactoryBean createWebSocketContainer() { ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean(); container.setMaxTextMessageBufferSize(8192); container.setMaxBinaryMessageBufferSize(8192); return container; } @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myHandler(), "/socket"); } @Bean AppSocket
  • 4. public ServletServerContainerFactoryBean createWebSocketContainer() { ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean(); container.setMaxTextMessageBufferSize(8192); container.setMaxBinaryMessageBufferSize(8192); return container; } @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myHandler(), "/socket"); } @Bean public WebSocketHandler myHandler() { return new AppSocket(); } @Bean public TaskScheduler taskScheduler() { return new ConcurrentTaskScheduler(Executors. newSingleThreadScheduledExecutor()); AppSocket
  • 5. container.setMaxTextMessageBufferSize(8192); container.setMaxBinaryMessageBufferSize(8192); return container; } @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myHandler(), "/socket"); } @Bean public WebSocketHandler myHandler() { return new AppSocket(); } @Bean public TaskScheduler taskScheduler() { return new ConcurrentTaskScheduler(Executors. newSingleThreadScheduledExecutor()); } } AppSocket
  • 6. import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.TextWebSocketHandler; public class AppSocket extends TextWebSocketHandler { private static Logger log = Logger.getLogger(AppSocket.class.getName()); private static final Map<String, List<WebSocketSession>> clients = new HashMap<>(); @Autowired private UserService users; private static boolean sendToToken(String token, String json) { List<WebSocketSession> ws = clients.get(token); WebSocketSession currentSocket = null; try { if(ws != null) { TextMessage t = new TextMessage(json); List<WebSocketSession> removeQueue = null; for(WebSocketSession w : ws) { currentSocket = w; AppSocket
  • 7. import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.TextWebSocketHandler; public class AppSocket extends TextWebSocketHandler { private static Logger log = Logger.getLogger(AppSocket.class.getName()); private static final Map<String, List<WebSocketSession>> clients = new HashMap<>(); @Autowired private UserService users; private static boolean sendToToken(String token, String json) { List<WebSocketSession> ws = clients.get(token); WebSocketSession currentSocket = null; try { if(ws != null) { TextMessage t = new TextMessage(json); List<WebSocketSession> removeQueue = null; for(WebSocketSession w : ws) { currentSocket = w; AppSocket
  • 8. public class AppSocket extends TextWebSocketHandler { private static Logger log = Logger.getLogger(AppSocket.class.getName()); private static final Map<String, List<WebSocketSession>> clients = new HashMap<>(); @Autowired private UserService users; private static boolean sendToToken(String token, String json) { List<WebSocketSession> ws = clients.get(token); WebSocketSession currentSocket = null; try { if(ws != null) { TextMessage t = new TextMessage(json); List<WebSocketSession> removeQueue = null; for(WebSocketSession w : ws) { currentSocket = w; if(currentSocket.isOpen()) { w.sendMessage(t); AppSocket
  • 9. new HashMap<>(); @Autowired private UserService users; private static boolean sendToToken(String token, String json) { List<WebSocketSession> ws = clients.get(token); WebSocketSession currentSocket = null; try { if(ws != null) { TextMessage t = new TextMessage(json); List<WebSocketSession> removeQueue = null; for(WebSocketSession w : ws) { currentSocket = w; if(currentSocket.isOpen()) { w.sendMessage(t); return true; } else { if(removeQueue == null) { removeQueue = new ArrayList<>(); } removeQueue.add(w); AppSocket
  • 10. new HashMap<>(); @Autowired private UserService users; private static boolean sendToToken(String token, String json) { List<WebSocketSession> ws = clients.get(token); WebSocketSession currentSocket = null; try { if(ws != null) { TextMessage t = new TextMessage(json); List<WebSocketSession> removeQueue = null; for(WebSocketSession w : ws) { currentSocket = w; if(currentSocket.isOpen()) { w.sendMessage(t); return true; } else { if(removeQueue == null) { removeQueue = new ArrayList<>(); } removeQueue.add(w); AppSocket
  • 11. private static boolean sendToToken(String token, String json) { List<WebSocketSession> ws = clients.get(token); WebSocketSession currentSocket = null; try { if(ws != null) { TextMessage t = new TextMessage(json); List<WebSocketSession> removeQueue = null; for(WebSocketSession w : ws) { currentSocket = w; if(currentSocket.isOpen()) { w.sendMessage(t); return true; } else { if(removeQueue == null) { removeQueue = new ArrayList<>(); } removeQueue.add(w); } } if(removeQueue != null) { for(WebSocketSession w : removeQueue) { ws.remove(w); AppSocket
  • 12. private static boolean sendToToken(String token, String json) { List<WebSocketSession> ws = clients.get(token); WebSocketSession currentSocket = null; try { if(ws != null) { TextMessage t = new TextMessage(json); List<WebSocketSession> removeQueue = null; for(WebSocketSession w : ws) { currentSocket = w; if(currentSocket.isOpen()) { w.sendMessage(t); return true; } else { if(removeQueue == null) { removeQueue = new ArrayList<>(); } removeQueue.add(w); } } if(removeQueue != null) { for(WebSocketSession w : removeQueue) { ws.remove(w); AppSocket
  • 13. List<WebSocketSession> ws = clients.get(token); WebSocketSession currentSocket = null; try { if(ws != null) { TextMessage t = new TextMessage(json); List<WebSocketSession> removeQueue = null; for(WebSocketSession w : ws) { currentSocket = w; if(currentSocket.isOpen()) { w.sendMessage(t); return true; } else { if(removeQueue == null) { removeQueue = new ArrayList<>(); } removeQueue.add(w); } } if(removeQueue != null) { for(WebSocketSession w : removeQueue) { ws.remove(w); } } AppSocket
  • 14. List<WebSocketSession> ws = clients.get(token); WebSocketSession currentSocket = null; try { if(ws != null) { TextMessage t = new TextMessage(json); List<WebSocketSession> removeQueue = null; for(WebSocketSession w : ws) { currentSocket = w; if(currentSocket.isOpen()) { w.sendMessage(t); return true; } else { if(removeQueue == null) { removeQueue = new ArrayList<>(); } removeQueue.add(w); } } if(removeQueue != null) { for(WebSocketSession w : removeQueue) { ws.remove(w); } } AppSocket
  • 15. List<WebSocketSession> ws = clients.get(token); WebSocketSession currentSocket = null; try { if(ws != null) { TextMessage t = new TextMessage(json); List<WebSocketSession> removeQueue = null; for(WebSocketSession w : ws) { currentSocket = w; if(currentSocket.isOpen()) { w.sendMessage(t); return true; } else { if(removeQueue == null) { removeQueue = new ArrayList<>(); } removeQueue.add(w); } } if(removeQueue != null) { for(WebSocketSession w : removeQueue) { ws.remove(w); } } AppSocket
  • 16. removeQueue = new ArrayList<>(); } removeQueue.add(w); } } if(removeQueue != null) { for(WebSocketSession w : removeQueue) { ws.remove(w); } } } else { log.warning("No WS connections for token: " + token); } } catch(IOException err) { log.log(Level.SEVERE, "Exception during sending message", err); if(currentSocket != null && ws != null) { ws.remove(currentSocket); if(ws.isEmpty()) { clients.remove(token); } } } AppSocket
  • 17. removeQueue.add(w); } } if(removeQueue != null) { for(WebSocketSession w : removeQueue) { ws.remove(w); } } } else { log.warning("No WS connections for token: " + token); } } catch(IOException err) { log.log(Level.SEVERE, "Exception during sending message", err); if(currentSocket != null && ws != null) { ws.remove(currentSocket); if(ws.isEmpty()) { clients.remove(token); } } } return false; } AppSocket
  • 18. } public static boolean sendMessage(String token, String json) { return sendToToken(token, json); } @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { JsonParser parser = JsonParserFactory.getJsonParser(); Map<String, Object> m = parser.parseMap(message.getPayload()); String type = (String)m.get("t"); if(type != null) { if(type.equals("init")) { String token = (String)m.get("tok"); Number time = (Number)m.get("time"); List<WebSocketSession> l = clients.get(token); if(l == null) { l = new ArrayList<>(); clients.put(token, l); } l.add(session); AppSocket
  • 19. public static boolean sendMessage(String token, String json) { return sendToToken(token, json); } @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { JsonParser parser = JsonParserFactory.getJsonParser(); Map<String, Object> m = parser.parseMap(message.getPayload()); String type = (String)m.get("t"); if(type != null) { if(type.equals("init")) { String token = (String)m.get("tok"); Number time = (Number)m.get("time"); List<WebSocketSession> l = clients.get(token); if(l == null) { l = new ArrayList<>(); clients.put(token, l); } l.add(session); return; } } else { AppSocket
  • 20. @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { JsonParser parser = JsonParserFactory.getJsonParser(); Map<String, Object> m = parser.parseMap(message.getPayload()); String type = (String)m.get("t"); if(type != null) { if(type.equals("init")) { String token = (String)m.get("tok"); Number time = (Number)m.get("time"); List<WebSocketSession> l = clients.get(token); if(l == null) { l = new ArrayList<>(); clients.put(token, l); } l.add(session); return; } } else { String typing = (String)m.get("typing"); String sentTo = (String)m.get("sentTo"); if(typing != null) { String authorId = (String)m.get("authorId"); AppSocket
  • 21. @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { JsonParser parser = JsonParserFactory.getJsonParser(); Map<String, Object> m = parser.parseMap(message.getPayload()); String type = (String)m.get("t"); if(type != null) { if(type.equals("init")) { String token = (String)m.get("tok"); Number time = (Number)m.get("time"); List<WebSocketSession> l = clients.get(token); if(l == null) { l = new ArrayList<>(); clients.put(token, l); } l.add(session); return; } } else { String typing = (String)m.get("typing"); String sentTo = (String)m.get("sentTo"); if(typing != null) { String authorId = (String)m.get("authorId"); AppSocket
  • 22. l = new ArrayList<>(); clients.put(token, l); } l.add(session); return; } } else { String typing = (String)m.get("typing"); String sentTo = (String)m.get("sentTo"); if(typing != null) { String authorId = (String)m.get("authorId"); users.userTyping(authorId, sentTo, true); } else { String id = (String)m.get("id"); if(id == null) { users.sendMessage(sentTo, m); } else { users.sendJSONTo(sentTo, message.getPayload()); } } } } AppSocket
  • 23. l = new ArrayList<>(); clients.put(token, l); } l.add(session); return; } } else { String typing = (String)m.get("typing"); String sentTo = (String)m.get("sentTo"); if(typing != null) { String authorId = (String)m.get("authorId"); users.userTyping(authorId, sentTo, true); } else { String id = (String)m.get("id"); if(id == null) { users.sendMessage(sentTo, m); } else { users.sendJSONTo(sentTo, message.getPayload()); } } } } AppSocket
  • 24. @Override public void handleTransportError(WebSocketSession wss, Throwable thrwbl) throws Exception { log.log(Level.SEVERE, "Error during transport", thrwbl); } @Override public void afterConnectionClosed(WebSocketSession wss, CloseStatus cs) throws Exception { for(String s : clients.keySet()) { List<WebSocketSession> wl = clients.get(s); for(WebSocketSession w : wl) { if(w == wss) { wl.remove(w); if(wl.isEmpty()) { clients.remove(s); } return; } } } } AppSocket
  • 25. @Override public void afterConnectionClosed(WebSocketSession wss, CloseStatus cs) throws Exception { for(String s : clients.keySet()) { List<WebSocketSession> wl = clients.get(s); for(WebSocketSession w : wl) { if(w == wss) { wl.remove(w); if(wl.isEmpty()) { clients.remove(s); } return; } } } } @Override public boolean supportsPartialMessages() { return false; } } AppSocket