SlideShare a Scribd company logo
WebSockets in Java
From n00b to pro

22 Dec 2013, JavaDay, Pance Cavkovski
Theory

- WebSocket is a protocol providing full-duplex
communications over a single TCP
connection.
- Standardized by IETF as RFC 6455
- Designed to be implemented in web browsers
and web servers
- HTTP Upgrade request for initiating
connection

* Contents from Wikipedia: http://en.wikipedia.org/wiki/Web_sockets

GET /mychat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat
Sec-WebSocket-Version: 13
Origin: http://example.com
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept:
HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat

Netcetera | 2
Sample application

https://github.com/hsilomedus/web-sockets-samples
Netcetera | 3
public static void main(String[] args)
JavaWebSocket (web-socket-samples/javawebsockets)
public class Main extends WebSocketServer {
public Main() {
super(new InetSocketAddress(8887));
}
@Override
public void onOpen(WebSocket conn,
ClientHandshake handshake) {
//Handle new connection here
conn.send(“{”connected”: true}”);
}
@Override
public void onMessage(WebSocket conn,
String message) {
//Handle client received message here
}
* https://github.com/TooTallNate/Java-WebSocket

@Override
public void onClose(WebSocket conn, int code,
String reason, boolean remote) {
//Handle closing connection here
}
@Override
public void onError(WebSocket conn,
Exception exc) {
//Handle error during transport here
}
public static void main(String[] args) {
Main server = new Main();
server.start();
}
}
Netcetera | 4
Client
JavaScript WebSocket API
var socket = new WebSocket(“ws://localhost:8887”);
socket.onopen = function() {
//event handler when the connection has been established
socket.send(nickname);
};
socket.onmessage = function(message) {
//event handler when data has been received from the server
alert(message.data);
};
socket.onclose = function() {
//event handler when the socket has been properly closed
}
socket.onerror = function() {
//event handler when an error has occurred during communication
}
Netcetera | 5
Java EE 7: JSR 356 + Java7
Tomcat (>7.0.43) (web-socket-samples/eesockets)
@ServerEndpoint(“/chat”)
public class EESocketChatEndpoint {
@OnOpen
public void onOpen(Session session) {
//Handle new connection here
session.getBasicRemote()
.sendText(“{”connected”: true}”);
}
@OnMessage
public void onMessage(String message) {
//Handle client received message here
}

@OnClose
public void onClose(Session session,
CloseReason reason) {
//Handle closing connection here
}
@OnError
public void onError(Session session,
Throwable throwable) {
//Handle error during transport here
}
}

* JSR 356, Java API for WebSocket: http://www.oracle.com/technetwork/articles/java/jsr356-1937161.html (javax.websocket.*)

Netcetera | 6
Client
Pretty much the same JavaScript WebSocket API
var socket = new WebSocket(”ws://” + document.domain + “:8080/eesockets/chat”);
socket.onopen = function() {
//event handler when the connection has been established
socket.send(nickname);
};
socket.onmessage = function(message) {
//event handler when data has been received from the server
alert(message.data);
};
socket.onclose = function() {
//event handler when the socket has been properly closed
}
socket.onerror = function() {
//event handler when an error has occurred during communication
}
Netcetera | 7
Spring4
Static dispatcher servlet config
public class DispatcherServletInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() { return null; }
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] {WebConfig.class};
}
@Override
protected String[] getServletMappings() { return new String[]{“/”}; }
@Override
protected void customizeRegistration(Dynamic registration) {
registration.setInitParameter(“dispatchOptionsRequest”, “true”);
}
}
Netcetera | 8
Spring4
Static context config
@Configuration
@EnableWebMvc
@EnableWebSocket
@ComponentScan(basePackages={“mk.hsilomedus.springsockets.service”})
public class WebConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(chatWenSocketHandler(), “/chat”).withSockJS();
}
@Bean
public WebSocketHandler chatWebSocketHandler() {
return new PerConnectionWebSocketHandler(ChatWebSocketHandler.class);
}
@Override
public void configureDefaultServletHandling(DefaultServerHandlerConfigurer configurer) {
configurer.enable();
}
Netcetera | 9
}
Spring4
WebSocketHandler (web-socket-samples/springsockets)
public class ChatWebSocketHandler extends
TextWebSocketHandler {
@Override
public void afterConnectionEstablished(
WebSocketSession session) {
//Handle new connection here
session.sendMessage(
“{”connected”: true}”);
}
@Override
public void handleTextMessage(
WebSocketSession session,
TextMessage message) {
//Handle message.getPayload() here
}
* http://blog.gopivotal.com/products/websocket-architecture-in-spring-4-0

@Override
public void afterConnectionClosed(
WebSocketSession session,
CloseStatus status) {
//Handle closing connection here
}
@Override
public void handleTransportError(
WebSocketSession session,
Throwable exception) {
//Handle error during transport here
}
}

Netcetera | 10
Client
SockJS
//I can now fallback to longpoll and do IE9!!!
var socket = new SockJS(”http://” + document.domain + “:8080/springsockets/chat”);
socket.onopen = function() {
//event handler when the connection has been established
socket.send(nickname);
};
socket.onmessage = function(message) {
//event handler when data has been received from the server
alert(message.data);
};
socket.onclose = function() {
//event handler when the socket has been properly closed
}
socket.onerror = function() {
//event handler when an error has occurred during communication
}
Netcetera | 11
socket.close();
Thanks for your attention
- The whole source code is available on github:
https://github.com/hsilomedus/web-sockets-samples
- /javawebsockets – with the JavaWebSocket library
- /eesockets – with Tomcat 7.0.47
- /springsockets – with Spring4 RC2 and Tomcat 7.0.47
- All three are eclipse projects running on Java7
- Questions?
- https://twitter.com/hsilomedus, http://hsilomedus.me/
Netcetera | 12

More Related Content

What's hot (20)

PDF
Building Real-Time Applications with Android and WebSockets
Sergi Almar i Graupera
 
KEY
The HTML5 WebSocket API
David Lindkvist
 
PPTX
Php push notifications
Mohammed Shurrab
 
PPT
HTML5 WebSocket: The New Network Stack for the Web
Peter Lubbers
 
PPTX
Intro to WebSockets
Gaurav Oberoi
 
ZIP
Websocket protocol overview
allenmeng
 
PPTX
Websockets on the JVM: Atmosphere to the rescue!
jfarcand
 
PPTX
Reverse ajax in 2014
Nenad Pecanac
 
PPTX
Ws
Sunghan Kim
 
PDF
GWT Web Socket and data serialization
GWTcon
 
KEY
Dancing with websocket
Damien Krotkine
 
PPS
J web socket
Hiroshi Ochi
 
PDF
Using Websockets with Play!
Andrew Conner
 
PPTX
The Atmosphere Framework
jfarcand
 
PPTX
WebSockets in JEE 7
Shahzad Badar
 
ODP
Using Websockets in Play !
Knoldus Inc.
 
PPTX
Large scale web socket system with AWS and Web socket
Le Kien Truc
 
PPT
Writing highly scalable WebSocket using the Atmosphere Framework and Scala
jfarcand
 
PPTX
Building WebSocket and Server Side Events Applications using Atmosphere
jfarcand
 
PPTX
Websockets in Node.js - Making them reliable and scalable
Gareth Marland
 
Building Real-Time Applications with Android and WebSockets
Sergi Almar i Graupera
 
The HTML5 WebSocket API
David Lindkvist
 
Php push notifications
Mohammed Shurrab
 
HTML5 WebSocket: The New Network Stack for the Web
Peter Lubbers
 
Intro to WebSockets
Gaurav Oberoi
 
Websocket protocol overview
allenmeng
 
Websockets on the JVM: Atmosphere to the rescue!
jfarcand
 
Reverse ajax in 2014
Nenad Pecanac
 
GWT Web Socket and data serialization
GWTcon
 
Dancing with websocket
Damien Krotkine
 
J web socket
Hiroshi Ochi
 
Using Websockets with Play!
Andrew Conner
 
The Atmosphere Framework
jfarcand
 
WebSockets in JEE 7
Shahzad Badar
 
Using Websockets in Play !
Knoldus Inc.
 
Large scale web socket system with AWS and Web socket
Le Kien Truc
 
Writing highly scalable WebSocket using the Atmosphere Framework and Scala
jfarcand
 
Building WebSocket and Server Side Events Applications using Atmosphere
jfarcand
 
Websockets in Node.js - Making them reliable and scalable
Gareth Marland
 

Viewers also liked (20)

PDF
Java sockets
Stephen Pradeep
 
PPT
Present Continuous
lupitath09
 
PDF
Garbage Collection without Paging
Emery Berger
 
PDF
Open and online: connections, community and reality
Catherine Cronin
 
PPTX
Copenhagen Open For Connections Dias
Wonderful Copenhagen
 
PPTX
Multithreaded programming
Sonam Sharma
 
PPTX
Insert a Page Number in the Running Head
Amy Lynn Hess
 
PPTX
Presentiaon task sheduling first come first serve FCFS
Ahmed Salah
 
PPS
The Look Of Love
Graham Bennett
 
PPTX
4 character encoding-ascii
irdginfo
 
PPT
Ascii codes
Saddam Hussain Soomro
 
PDF
Process' Virtual Address Space in GNU/Linux
Varun Mahajan
 
PPT
C scan scheduling 50 2
myrajendra
 
KEY
3장. Garbage Collection
김 한도
 
PPTX
Paging-R.D.Sivakumar
Sivakumar R D .
 
PPT
Look scheduling.51
myrajendra
 
PPT
Sstf scheduling.50
myrajendra
 
PPT
Basic Garbage Collection Techniques
An Khuong
 
PPT
Fcfs scheduling
myrajendra
 
PPT
message passing
Ashish Kumar
 
Java sockets
Stephen Pradeep
 
Present Continuous
lupitath09
 
Garbage Collection without Paging
Emery Berger
 
Open and online: connections, community and reality
Catherine Cronin
 
Copenhagen Open For Connections Dias
Wonderful Copenhagen
 
Multithreaded programming
Sonam Sharma
 
Insert a Page Number in the Running Head
Amy Lynn Hess
 
Presentiaon task sheduling first come first serve FCFS
Ahmed Salah
 
The Look Of Love
Graham Bennett
 
4 character encoding-ascii
irdginfo
 
Process' Virtual Address Space in GNU/Linux
Varun Mahajan
 
C scan scheduling 50 2
myrajendra
 
3장. Garbage Collection
김 한도
 
Paging-R.D.Sivakumar
Sivakumar R D .
 
Look scheduling.51
myrajendra
 
Sstf scheduling.50
myrajendra
 
Basic Garbage Collection Techniques
An Khuong
 
Fcfs scheduling
myrajendra
 
message passing
Ashish Kumar
 
Ad

Similar to Web sockets in Java (20)

PPT
Servlet 3.0
Minh Hoang
 
PDF
Lecture 6 Web Sockets
Fahad Golra
 
PDF
Chapter 27 Networking - Deitel & Deitel
CSDeptSriKaliswariCo
 
ODP
Networking and Data Access with Eqela
jobandesther
 
PDF
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
Matt Raible
 
KEY
Websockets - DevFestX May 19, 2012
Sameer Segal
 
PDF
Lecture 10 Networking on Mobile Devices
Maksym Davydov
 
PPTX
Servlets
Geethu Mohan
 
PDF
Android Networking
Maksym Davydov
 
PPTX
Html5 websockets
AbhishekMondal42
 
PDF
Managing user's data with Spring Session
David Gómez García
 
PDF
Introduction tomcat7 servlet3
JavaEE Trainers
 
PDF
React Native EU 2021 - Creating a VoIP app in React Native - the beginner's g...
Wojciech Kwiatek
 
PDF
Go 1.8 'new' networking features
strikr .
 
PDF
Speed up your Web applications with HTML5 WebSockets
Yakov Fain
 
PPT
Socket Programming - nitish nagar
Nitish Nagar
 
PPT
Java Servlets
BG Java EE Course
 
PPTX
Solving anything in VCL
Fastly
 
PDF
Container orchestration from theory to practice
Docker, Inc.
 
PPT
Building+restful+webservice
lonegunman
 
Servlet 3.0
Minh Hoang
 
Lecture 6 Web Sockets
Fahad Golra
 
Chapter 27 Networking - Deitel & Deitel
CSDeptSriKaliswariCo
 
Networking and Data Access with Eqela
jobandesther
 
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
Matt Raible
 
Websockets - DevFestX May 19, 2012
Sameer Segal
 
Lecture 10 Networking on Mobile Devices
Maksym Davydov
 
Servlets
Geethu Mohan
 
Android Networking
Maksym Davydov
 
Html5 websockets
AbhishekMondal42
 
Managing user's data with Spring Session
David Gómez García
 
Introduction tomcat7 servlet3
JavaEE Trainers
 
React Native EU 2021 - Creating a VoIP app in React Native - the beginner's g...
Wojciech Kwiatek
 
Go 1.8 'new' networking features
strikr .
 
Speed up your Web applications with HTML5 WebSockets
Yakov Fain
 
Socket Programming - nitish nagar
Nitish Nagar
 
Java Servlets
BG Java EE Course
 
Solving anything in VCL
Fastly
 
Container orchestration from theory to practice
Docker, Inc.
 
Building+restful+webservice
lonegunman
 
Ad

More from Pance Cavkovski (9)

PPTX
Jprofessionals co create the future of your city
Pance Cavkovski
 
PPTX
Gluing the IoT world with Java and LoRaWAN (Jfokus 2018)
Pance Cavkovski
 
PPTX
Gluing the iot world (ICT)
Pance Cavkovski
 
PPTX
Gluing the IoT world with Java and LoRaWAN
Pance Cavkovski
 
ODP
VDB16 - DIY Java & Kubernetes
Pance Cavkovski
 
ODP
DIY Java & Kubernetes
Pance Cavkovski
 
PPTX
Connected hardware for Software Engineers 101
Pance Cavkovski
 
PPTX
Hands on Java8 and RaspberryPi
Pance Cavkovski
 
PPTX
Micro and moblile: Java on the Raspberry Pi
Pance Cavkovski
 
Jprofessionals co create the future of your city
Pance Cavkovski
 
Gluing the IoT world with Java and LoRaWAN (Jfokus 2018)
Pance Cavkovski
 
Gluing the iot world (ICT)
Pance Cavkovski
 
Gluing the IoT world with Java and LoRaWAN
Pance Cavkovski
 
VDB16 - DIY Java & Kubernetes
Pance Cavkovski
 
DIY Java & Kubernetes
Pance Cavkovski
 
Connected hardware for Software Engineers 101
Pance Cavkovski
 
Hands on Java8 and RaspberryPi
Pance Cavkovski
 
Micro and moblile: Java on the Raspberry Pi
Pance Cavkovski
 

Recently uploaded (20)

PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PDF
July Patch Tuesday
Ivanti
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Top Managed Service Providers in Los Angeles
Captain IT
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
July Patch Tuesday
Ivanti
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 

Web sockets in Java

  • 1. WebSockets in Java From n00b to pro 22 Dec 2013, JavaDay, Pance Cavkovski
  • 2. Theory - WebSocket is a protocol providing full-duplex communications over a single TCP connection. - Standardized by IETF as RFC 6455 - Designed to be implemented in web browsers and web servers - HTTP Upgrade request for initiating connection * Contents from Wikipedia: http://en.wikipedia.org/wiki/Web_sockets GET /mychat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: chat Sec-WebSocket-Version: 13 Origin: http://example.com HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= Sec-WebSocket-Protocol: chat Netcetera | 2
  • 4. public static void main(String[] args) JavaWebSocket (web-socket-samples/javawebsockets) public class Main extends WebSocketServer { public Main() { super(new InetSocketAddress(8887)); } @Override public void onOpen(WebSocket conn, ClientHandshake handshake) { //Handle new connection here conn.send(“{”connected”: true}”); } @Override public void onMessage(WebSocket conn, String message) { //Handle client received message here } * https://github.com/TooTallNate/Java-WebSocket @Override public void onClose(WebSocket conn, int code, String reason, boolean remote) { //Handle closing connection here } @Override public void onError(WebSocket conn, Exception exc) { //Handle error during transport here } public static void main(String[] args) { Main server = new Main(); server.start(); } } Netcetera | 4
  • 5. Client JavaScript WebSocket API var socket = new WebSocket(“ws://localhost:8887”); socket.onopen = function() { //event handler when the connection has been established socket.send(nickname); }; socket.onmessage = function(message) { //event handler when data has been received from the server alert(message.data); }; socket.onclose = function() { //event handler when the socket has been properly closed } socket.onerror = function() { //event handler when an error has occurred during communication } Netcetera | 5
  • 6. Java EE 7: JSR 356 + Java7 Tomcat (>7.0.43) (web-socket-samples/eesockets) @ServerEndpoint(“/chat”) public class EESocketChatEndpoint { @OnOpen public void onOpen(Session session) { //Handle new connection here session.getBasicRemote() .sendText(“{”connected”: true}”); } @OnMessage public void onMessage(String message) { //Handle client received message here } @OnClose public void onClose(Session session, CloseReason reason) { //Handle closing connection here } @OnError public void onError(Session session, Throwable throwable) { //Handle error during transport here } } * JSR 356, Java API for WebSocket: http://www.oracle.com/technetwork/articles/java/jsr356-1937161.html (javax.websocket.*) Netcetera | 6
  • 7. Client Pretty much the same JavaScript WebSocket API var socket = new WebSocket(”ws://” + document.domain + “:8080/eesockets/chat”); socket.onopen = function() { //event handler when the connection has been established socket.send(nickname); }; socket.onmessage = function(message) { //event handler when data has been received from the server alert(message.data); }; socket.onclose = function() { //event handler when the socket has been properly closed } socket.onerror = function() { //event handler when an error has occurred during communication } Netcetera | 7
  • 8. Spring4 Static dispatcher servlet config public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override protected Class<?>[] getRootConfigClasses() { return null; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] {WebConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{“/”}; } @Override protected void customizeRegistration(Dynamic registration) { registration.setInitParameter(“dispatchOptionsRequest”, “true”); } } Netcetera | 8
  • 9. Spring4 Static context config @Configuration @EnableWebMvc @EnableWebSocket @ComponentScan(basePackages={“mk.hsilomedus.springsockets.service”}) public class WebConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer { public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(chatWenSocketHandler(), “/chat”).withSockJS(); } @Bean public WebSocketHandler chatWebSocketHandler() { return new PerConnectionWebSocketHandler(ChatWebSocketHandler.class); } @Override public void configureDefaultServletHandling(DefaultServerHandlerConfigurer configurer) { configurer.enable(); } Netcetera | 9 }
  • 10. Spring4 WebSocketHandler (web-socket-samples/springsockets) public class ChatWebSocketHandler extends TextWebSocketHandler { @Override public void afterConnectionEstablished( WebSocketSession session) { //Handle new connection here session.sendMessage( “{”connected”: true}”); } @Override public void handleTextMessage( WebSocketSession session, TextMessage message) { //Handle message.getPayload() here } * http://blog.gopivotal.com/products/websocket-architecture-in-spring-4-0 @Override public void afterConnectionClosed( WebSocketSession session, CloseStatus status) { //Handle closing connection here } @Override public void handleTransportError( WebSocketSession session, Throwable exception) { //Handle error during transport here } } Netcetera | 10
  • 11. Client SockJS //I can now fallback to longpoll and do IE9!!! var socket = new SockJS(”http://” + document.domain + “:8080/springsockets/chat”); socket.onopen = function() { //event handler when the connection has been established socket.send(nickname); }; socket.onmessage = function(message) { //event handler when data has been received from the server alert(message.data); }; socket.onclose = function() { //event handler when the socket has been properly closed } socket.onerror = function() { //event handler when an error has occurred during communication } Netcetera | 11
  • 12. socket.close(); Thanks for your attention - The whole source code is available on github: https://github.com/hsilomedus/web-sockets-samples - /javawebsockets – with the JavaWebSocket library - /eesockets – with Tomcat 7.0.47 - /springsockets – with Spring4 RC2 and Tomcat 7.0.47 - All three are eclipse projects running on Java7 - Questions? - https://twitter.com/hsilomedus, http://hsilomedus.me/ Netcetera | 12