Introduction. Using HTML 5 WebSockets we can dramatically reduce unnecessary network traffic and latency Haim Michael. All Rights Reserved.

Similar documents
Using Java with HTML5 and CSS3 (+ the whole HTML5 world: WebSockets, SVG, etc...)

Kaazing. Connect. Everything. WebSocket The Web Communication Revolution

Here are a few easy steps to create a simple timeline. Open up your favorite text or HTML editor and start creating an HTML file.

IBM Bluemix Node-RED Watson Starter

The realtime web: HTTP/1.1 to WebSocket, SPDY and beyond. Guillermo QCon. November 2012.

Asynchronous WebSockets using Django

Introduction Haim Michael. All Rights Reserved.

WebSocket and Java EE: A State of the Union

Admin Notes. Javascript API. Release 5.1 December 2010

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

An implementation of Tree Panel component in EXT JS 4.0

Kaazing Gateway. Open Source HTML 5 Web Socket Server

VidyoEngage for Genesys Widgets

The Servlet Life Cycle

Advanced Internet Technology Lab # 4 Servlets

SEEM4540 Open Systems for E-Commerce Lecture 11 Advanced Topics

Copyright 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13

JAVA SERVLET. Server-side Programming INTRODUCTION

Live Agent Developer's Guide

Kaazing Gateway: An Open Source

Implementing a chat button on TECHNICAL PAPER

New frontier of responsive & device-friendly web sites

IERG 4080 Building Scalable Internet-based Services

FUSE Ajax Tutorial. 07/06 Version 1.2

TAG STYLE SELECTORS. div Think of this as a box that contains things, such as text or images. It can also just be a

Positioning in CSS: There are 5 different ways we can set our position:

Configuring Hotspots

Microsoft Developing ASP.NET MVC 4 Web Applications. Download Full Version :

IEMS 5722 Mobile Network Programming and Distributed Server Architecture

INTRODUCTION TO SERVLETS AND WEB CONTAINERS. Actions in Accord with All the Laws of Nature

Page Layout. 4.1 Styling Page Sections 4.2 Introduction to Layout 4.3 Floating Elements 4.4 Sizing and Positioning

Copy and Paste the scripts below into the Alter Response Replacement field:

Create a cool image gallery using CSS visibility and positioning property

Speed up Your Web Applications with HTML5 WebSockets. Yakov Fain, Farata Systems, USA

Networking & The Web. HCID 520 User Interface Software & Technology

Servlets by Example. Joe Howse 7 June 2011

jmaki Overview Sang Shin Java Technology Architect Sun Microsystems, Inc.

Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

CT51 WEB TECHNOLOGY ALCCS-FEB 2014

Developing Web Views for VMware vcenter Orchestrator. vrealize Orchestrator 5.5

Developing Web Views for VMware vcenter Orchestrator

Hoster: openload.co - Free PLUGIN_DEFECT-Error: 08d a1830b60ab13ddec9a2ff6

Unveiling the Basics of CSS and how it relates to the DataFlex Web Framework

Building next-gen Web Apps with WebSocket. Copyright Kaazing Corporation. All rights reserved.

1.2 * allow custom user list to be passed in * publish changes to a channel

ToDoList. 1.2 * allow custom user list to be passed in * publish changes to a channel ***/

Asema IoT Central Integration and migration. English

COURSE DETAILS: CORE AND ADVANCE JAVA Core Java

Chapter 1 Getting Started with HTML 5 1. Chapter 2 Introduction to New Elements in HTML 5 21

Web Programming Paper Solution (Chapter wise)

Comet and WebSocket Web Applications How to Scale Server-Side Event-Driven Scenarios

Using CSS for page layout

CSC443: Web Programming 2

I Can t Believe It s Not

Contents at a Glance

Connecting the RISC Client to non-javascriptinterfaces

CSE 143: Computer Programming II Spring 2015 HW2: HTMLManager (due Thursday, April 16, :30pm)

Scriptaculous Stuart Halloway

Java Training For Six Weeks

LAB 1 PREPARED BY : DR. AJUNE WANIS ISMAIL FACULTY OF COMPUTING UNIVERSITI TEKNOLOGI MALAYSIA

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

django-session-security Documentation

Kamnoetvidya Science Academy. Object Oriented Programming using Java. Ferdin Joe John Joseph. Java Session

********************************************************************

First Simple Interactive JSP example

Advantage of JSP over Servlet

UNIVERSITY OF TORONTO Faculty of Arts and Science APRIL 2016 EXAMINATIONS. CSC309H1 S Programming on the Web Instructor: Ahmed Shah Mashiyat

Introduction to using HTML to design webpages

if(! list.contains(list.collect(params,'key'),'title')){ <div style="font-weight:bold;color:red;">"warning: A title field must be assigned.

Chapter 2 How to structure a web application with the MVC pattern

Session 11. Calling Servlets from Ajax. Lecture Objectives. Understand servlet response formats

Oracle Coherence and WebLogic 12c Delivering Real Time Push at Scale Steve Millidge

2. Follow the installation directions and install the server on ccc. 3. We will call the root of your installation as $TOMCAT_DIR

Google Web Toolkit Creating/using external JAR files

Writing Secure Chrome Apps and Extensions

Fundamentals of Web Technologies. Agenda: CSS Layout (Box Model) CSS Layout: Box Model. All HTML elements can be considered as a box or a container

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

Lecture 18. WebSocket

CS WEB TECHNOLOGY

Enabling Full-Duplex Communications in APEX

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Advanced Internet Technology Lab # 6

Schenker AB. Interface documentation Map integration

Paythru Remote Fields

Chapter 1 Introduction to Dreamweaver CS3 1. About Dreamweaver CS3 Interface...4. Creating New Webpages...10

What is the Box Model?

IT430- E-COMMERCE Solved MCQ(S) From Midterm Papers (1 TO 22 Lectures) BY Arslan Arshad

SNS COLLEGE OF ENGINEERING, Coimbatore

ObjectRiver. Metadata Compilers. WebSockets. JavaOne 2014 Steven Lemmo

Title: Dec 11 3:40 PM (1 of 11)

Ch04 JavaServer Pages (JSP)

Harnessing the Power of HTML5 WebSocket to Create Scalable Real-time Applications. Brian Albers & Peter Lubbers, Kaazing

PHP & My SQL Duration-4-6 Months

文字エンコーディングフィルタ 1. 更新履歴 2003/07/07 新規作成(第 0.1 版) 版 数 第 0.1 版 ページ番号 1

The Future of the Web: HTML 5, WebSockets, Comet and Server Sent Events

PDFreactor Migration Guide

Viostream Upload Widget

CHAPTER. Creating User-Defined Mashups

Discovery Service Infrastructure for Test- bädden

CS506 Web Design & Development Final Term Solved MCQs with Reference

Transcription:

WebSocket

Introduction HTML 5 WebSockets defines a communication channel that operates over the web and allows both direction communication over a single socket. Using HTML 5 WebSockets we can dramatically reduce unnecessary network traffic and latency

Introduction Using HTML 5 WebSckets, when data changes on the web server the web server can send a request to the client. We no longer need to implement a client that polls the server.

Web Browser Support We can easily check whether the web browser supports WebSocket or not. if (window.websocket) {...

Web Browser Support <div id="msg"></div> <script> if (window.websocket) { document.getelementbyid("msg").innerhtml = "browser supports"; else { document.getelementbyid("msg").innerhtml = "browser doesn't support"; </script>

Web Browser Support

Creating Web Socket var ws = new WebSocket( ws://services.abelski.com/samples ); We should pass over to the WebSocket constructor the URL address of the web socket server we intend to use. That address should start with 'ws' which stands for Web Sockets.

Call Back Functions ws.onopen = function(event) {... This function will be called when the connection is established.

Call Back Functions ws.onmessage = function(event) { alert(event.data);... This function will be called when a message arrives from the server.

Call Back Functions ws.onclose = function(event) {... This function will be called when the connection is closed.

Sending Data ws.postmessage( this is the message sent to the server ); We call the postmessage function in order to send a message

Close Connection ws.disconnect(); Calling this function will disconnect the connection with the server.

Sample The following code sample uses Java EE support for Web Sockets API as was implemented in Apache Tomcat.

Sample <!DOCTYPE html> <html> <head> <title>simple WebSockets Chat</title> <style type="text/css"> input#user-message { width: 360px #chat-messages { width: 355px; border: 1px solid #BBBBBB; height: 200px; overflow-y: scroll; padding: 4px; </style> </head> The HTML File

Sample <body> <div> <input type="text" placeholder="enter your message here" id="user-message"> <div id="chat-messages"></div> </div> <script type="text/javascript"> var chat = {; chat.socket = null; chat.connect = (function(host) { if ('WebSocket' in window) { chat.socket = new WebSocket(host); else if ('MozWebSocket' in window) { chat.socket = new MozWebSocket(host); else { return; chat.socket.onopen = function () { document.getelementbyid('user-message').onkeydown = function(event) { if (event.keycode == 13) { chat.sendmessage(); ; ;

Sample chat.socket.onclose = function () { document.getelementbyid('user-message').onkeydown = null; ; chat.socket.onmessage = function (message) { messages.add(message.data); ; ); chat.initialize = function() { if (window.location.protocol == 'http:') { chat.connect( 'ws://' + window.location.host + '/websocketproj/websocket'); else { chat.connect ('wss://' + window.location.host + '/websocketproj/websocket'); ; chat.sendmessage = (function() { var message = document.getelementbyid('user-message').value; if (message!= '') { chat.socket.send(message); document.getelementbyid('user-message').value = ''; );

Sample var messages = {; messages.add = (function(message) { var ob = document.getelementbyid('chat-messages'); var div = document.createelement('div'); div.style.wordwrap = 'break-word'; div.innerhtml = message; ob.appendchild(div); while (ob.childnodes.length > 10) { ob.removechild(ob.firstchild); ); chat.initialize(); </script> </body> </html>

Sample @WebServlet("/WebSocket") public class WebSocket extends WebSocketServlet { The Servlet File private static final long serialversionuid = 1L; private final AtomicInteger idgenerator = new AtomicInteger(0); private final Set<ChatUser> connections = new HashSet<ChatUser>(); @Override protected StreamInbound createwebsocketinbound(string subprotocol, HttpServletRequest request) { return new ChatUser(idGenerator.incrementAndGet()); private final class ChatUser extends MessageInbound { private final String username; private ChatUser(int id) { this.username = "guest #" + id;

Sample @Override protected void onopen(wsoutbound outbound) { connections.add(this); String message = username + " has joined the chat"; broadcast(message); @Override protected void onclose(int status) { connections.remove(this); String message = username + " has left the chat"; broadcast(message); @Override protected void onbinarymessage(bytebuffer message) throws IOException { throw new UnsupportedOperationException( "binary messages are not supported");

Sample @Override protected void ontextmessage(charbuffer message) throws IOException { // it would be best filter the message //... String str = username + ": " + message; broadcast(str); private void broadcast(string message) { for (ChatUser connection : connections) { try { CharBuffer buffer = CharBuffer.wrap(message); connection.getwsoutbound().writetextmessage(buffer); catch (IOException ignore) { //...

Sample

2008 Haim Michael 06/24/14 2008 Haim Michael 1 WebSocket

2008 Haim Michael 06/24/14 2008 Haim Michael 2 Introduction HTML 5 WebSockets defines a communication channel that operates over the web and allows both direction communication over a single socket. Using HTML 5 WebSockets we can dramatically reduce unnecessary network traffic and latency

2008 Haim Michael 06/24/14 2008 Haim Michael 3 Introduction Using HTML 5 WebSckets, when data changes on the web server the web server can send a request to the client. We no longer need to implement a client that polls the server.

2008 Haim Michael 06/24/14 2008 Haim Michael 4 Web Browser Support We can easily check whether the web browser supports WebSocket or not. if (window.websocket) {...

2008 Haim Michael 06/24/14 2008 Haim Michael 5 Web Browser Support <div id="msg"></div> <script> if (window.websocket) { document.getelementbyid("msg").innerhtml = "browser supports"; else { document.getelementbyid("msg").innerhtml = "browser doesn't support"; </script>

2008 Haim Michael 06/24/14 2008 Haim Michael 6 Web Browser Support

2008 Haim Michael 06/24/14 2008 Haim Michael 7 Creating Web Socket var ws = new WebSocket( ws://services.abelski.com/samples ); We should pass over to the WebSocket constructor the URL address of the web socket server we intend to use. That address should start with 'ws' which stands for Web Sockets.

2008 Haim Michael 06/24/14 2008 Haim Michael 8 Call Back Functions ws.onopen = function(event) {... This function will be called when the connection is established.

2008 Haim Michael 06/24/14 2008 Haim Michael 9 Call Back Functions ws.onmessage = function(event) { alert(event.data);... This function will be called when a message arrives from the server.

2008 Haim Michael 06/24/14 2008 Haim Michael 10 Call Back Functions ws.onclose = function(event) {... This function will be called when the connection is closed.

2008 Haim Michael 06/24/14 2008 Haim Michael 11 Sending Data ws.postmessage( this is the message sent to the server ); We call the postmessage function in order to send a message

2008 Haim Michael 06/24/14 2008 Haim Michael 12 Close Connection ws.disconnect(); Calling this function will disconnect the connection with the server.

2008 Haim Michael 06/24/14 2008 Haim Michael 13 Sample The following code sample uses Java EE support for Web Sockets API as was implemented in Apache Tomcat.

2008 Haim Michael 06/24/14 2008 Haim Michael 14 Sample <!DOCTYPE html> <html> <head> <title>simple WebSockets Chat</title> <style type="text/css"> input#user-message { width: 360px #chat-messages { width: 355px; border: 1px solid #BBBBBB; height: 200px; overflow-y: scroll; padding: 4px; </style> </head> The HTML File

2008 Haim Michael 06/24/14 2008 Haim Michael 15 Sample <body> <div> <input type="text" placeholder="enter your message here" id="user-message"> <div id="chat-messages"></div> </div> <script type="text/javascript"> var chat = {; chat.socket = null; chat.connect = (function(host) { if ('WebSocket' in window) { chat.socket = new WebSocket(host); else if ('MozWebSocket' in window) { chat.socket = new MozWebSocket(host); else { return; chat.socket.onopen = function () { document.getelementbyid('user-message').onkeydown = function(event) { if (event.keycode == 13) { chat.sendmessage(); ; ;

2008 Haim Michael 06/24/14 2008 Haim Michael 16 Sample chat.socket.onclose = function () { document.getelementbyid('user-message').onkeydown = null; ; chat.socket.onmessage = function (message) { messages.add(message.data); ; ); chat.initialize = function() { if (window.location.protocol == 'http:') { chat.connect( 'ws://' + window.location.host + '/websocketproj/websocket'); else { chat.connect ('wss://' + window.location.host + '/websocketproj/websocket'); ; chat.sendmessage = (function() { var message = document.getelementbyid('user-message').value; if (message!= '') { chat.socket.send(message); document.getelementbyid('user-message').value = ''; );

2008 Haim Michael 06/24/14 2008 Haim Michael 17 Sample var messages = {; messages.add = (function(message) { var ob = document.getelementbyid('chat-messages'); var div = document.createelement('div'); div.style.wordwrap = 'break-word'; div.innerhtml = message; ob.appendchild(div); while (ob.childnodes.length > 10) { ob.removechild(ob.firstchild); ); chat.initialize(); </script> </body> </html>

2008 Haim Michael 06/24/14 2008 Haim Michael 18 Sample @WebServlet("/WebSocket") public class WebSocket extends WebSocketServlet { private static final long serialversionuid = 1L; private final AtomicInteger idgenerator = new AtomicInteger(0); private final Set<ChatUser> connections = new HashSet<ChatUser>(); @Override protected StreamInbound createwebsocketinbound(string subprotocol, HttpServletRequest request) { return new ChatUser(idGenerator.incrementAndGet()); private final class ChatUser extends MessageInbound { private final String username; private ChatUser(int id) { this.username = "guest #" + id; The Servlet File

2008 Haim Michael 06/24/14 2008 Haim Michael 19 Sample @Override protected void onopen(wsoutbound outbound) { connections.add(this); String message = username + " has joined the chat"; broadcast(message); @Override protected void onclose(int status) { connections.remove(this); String message = username + " has left the chat"; broadcast(message); @Override protected void onbinarymessage(bytebuffer message) throws IOException { throw new UnsupportedOperationException( "binary messages are not supported");

2008 Haim Michael 06/24/14 2008 Haim Michael 20 Sample @Override protected void ontextmessage(charbuffer message) throws IOException { // it would be best filter the message //... String str = username + ": " + message; broadcast(str); private void broadcast(string message) { for (ChatUser connection : connections) { try { CharBuffer buffer = CharBuffer.wrap(message); connection.getwsoutbound().writetextmessage(buffer); catch (IOException ignore) { //...

2008 Haim Michael 06/24/14 2008 Haim Michael 21 Sample