SlideShare a Scribd company logo
1 of 25
Download to read offline
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.pdfShaiAlmog1
 
Qt & Webkit
Qt & WebkitQt & Webkit
Qt & WebkitQT-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 .NETBrandon Minnick, MBA
 
WebSockets Jump Start
WebSockets Jump StartWebSockets Jump Start
WebSockets Jump StartHaim 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 WindowsPhoneMohammad Shaker
 
Wonderful csom sps barcelona
Wonderful csom sps barcelonaWonderful csom sps barcelona
Wonderful csom sps barcelonaSonja Madsen
 
Correcting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await MistakesCorrecting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await MistakesBrandon Minnick, MBA
 
Websockets talk at Rubyconf Uruguay 2010
Websockets talk at Rubyconf Uruguay 2010Websockets talk at Rubyconf Uruguay 2010
Websockets talk at Rubyconf Uruguay 2010Ismael 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
 
Orsiso
OrsisoOrsiso
Orsisoe27
 
[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 productIgor Lozynskyi
 
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.pdffatoryoutlets
 
Starting Vert.x in Eclipse
Starting Vert.x in EclipseStarting Vert.x in Eclipse
Starting Vert.x in EclipseHyeonseok 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.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 
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.pdfShaiAlmog1
 

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

AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentationyogeshlabana357357
 
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfFrisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfAnubhavMangla3
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...FIDO Alliance
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptxFIDO Alliance
 
How to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in PakistanHow to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in Pakistandanishmna97
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfalexjohnson7307
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxFIDO Alliance
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewDianaGray10
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsLeah Henrickson
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
Navigating the Large Language Model choices_Ravi Daparthi
Navigating the Large Language Model choices_Ravi DaparthiNavigating the Large Language Model choices_Ravi Daparthi
Navigating the Large Language Model choices_Ravi DaparthiRaviKumarDaparthi
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxFIDO Alliance
 
CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)Wonjun Hwang
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch TuesdayIvanti
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxFIDO Alliance
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxjbellis
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctBrainSell Technologies
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxFIDO Alliance
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfdanishmna97
 

Recently uploaded (20)

AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfFrisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
How to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in PakistanHow to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in Pakistan
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdf
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overview
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Navigating the Large Language Model choices_Ravi Daparthi
Navigating the Large Language Model choices_Ravi DaparthiNavigating the Large Language Model choices_Ravi Daparthi
Navigating the Large Language Model choices_Ravi Daparthi
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptx
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptx
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 

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