Communicating via WebSockets

Slides:



Advertisements
Similar presentations
Dreamweaver Forms Overview. Forms – A Little Review Most user/webpage communication is one way, like this: Most user/webpage communication is one way,
Advertisements

Introduction to JavaScript
CHAPTER 6 GETTING USER INPUT WITH FORMS. LEARNING OBJECTIVES Use the and tag pair to define an HTML-based form Use the tag to create a text box for user.
CHAPTER 30 THE HTML 5 FORMS PROCESSING. LEARNING OBJECTIVES What the three form elements are How to use the HTML 5 tag to specify a list of words’ form.
Copyright 2004 Monash University IMS5401 Web-based Systems Development Topic 2: Elements of the Web (g) Interactivity.
Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: HTML and Client-side code.
CSCI 4550/8556 Computer Networks Comer, Chapter 3: Network Programming and Applications.
Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.
HTML Form Processing Learning Web Design – Chapter 9, pp Squirrel Book – Chapter 11, pp
Master’s course Bioinformatics Data Analysis and Tools Lecture 6: Internet Basics Centre for Integrative Bioinformatics.
Creating WordPress Websites. Creating a site on your computer Local server Local WordPress installation Setting Up Dreamweaver.
Dreamweaver Basics In this section you will learn how to:
Multiple Tiers in Action
CHAPTER 31 BROWSER IDENTIFICATION. LEARNING OBJECTIVES What a “hack” is and why Web developers try to avoid them How to determine a browser’s user-agent.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
Chapter 6 The World Wide Web. Web Pages Each page is an interactive multimedia publication It can include: text, graphics, music and videos Pages are.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
Writing a JavaScript User-Defined Function  A function is JavaScript code written to perform certain tasks repeatedly  Built-in functions.
The Internet The internet is simply a worldwide computer network that uses standardised communication protocols to transmit and exchange data.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
Chapter 29 World Wide Web & Browsing World Wide Web (WWW) is a distributed hypermedia (hypertext & graphics) on-line repository of information that users.
Microsoft FrontPage 2003 Illustrated Complete Integrating a Database with a Web Site.
Javascript JavaScript is what is called a client-side scripting language:  a programming language that runs inside an Internet browser (a browser is also.
Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side.
The Internet Salihu Ibrahim Dasuki (PhD) CSC102 INTRODUCTION TO COMPUTER SCIENCE.
Sockets A popular API for client-server interaction.
The Echo Server Problem. Contents  Basic Networking Concepts  The Echo Server Problem.
Chapter 17 The Need for HTML 5.
Introduction to.
BASIC CONCEPTS ON INTERNET &
Web Systems & Technologies
Introduction To Application Layer
Tonga Institute of Higher Education IT 141: Information Systems
Web Technologies Computing Science Thompson Rivers University
WebSockets: TCP in Javascript
Using JavaScript to Show an Alert
WWW and HTTP King Fahd University of Petroleum & Minerals
Jkelany Chat Project.
Chapter 2 Client/Server Applications
HISTORY OF COMPUTERS AND TECHNOLOGY
Some Common Terms The Internet is a network of computers spanning the globe. It is also called the World Wide Web. World Wide Web It is a collection of.
Application Layer Functionality and Protocols
Web UI Basics ITM 352.
Basic Contact Form user sends an
Application Layer Functionality and Protocols
Introduction to JavaScript
What is Cookie? Cookie is small information stored in text file on user’s hard drive by web server. This information is later used by web browser to retrieve.
Application Layer Functionality and Protocols
Tonga Institute of Higher Education IT 141: Information Systems
Of HTML, CSS and JavaScript
Introduction to JavaScript
Application Layer Functionality and Protocols
Introduction to HTML5 and WebSockets.
Web Socket Protocol.
Secure Web Programming
Tonga Institute of Higher Education IT 141: Information Systems
Teaching slides Chapter 6.
Application Layer Functionality and Protocols
Chapter 16 The World Wide Web.
Application Layer Functionality and Protocols
Application Layer Functionality and Protocols
Application Layer Functionality and Protocols
Application Layer Functionality and Protocols
Web Technologies Computing Science Thompson Rivers University
Client-Server Model: Requesting a Web Page
The Internet and Electronic mail
Introduction to JavaScript
HTTP and HTML HTML HTTP HTTP – Standardize the packaging
Web Forms.
Presentation transcript:

Communicating via WebSockets Chapter 29 Communicating via WebSockets

Learning Objectives How to test for browser support for the WebSocket API How to use a socket for data communication How to use the WebSocket API to send and receive messages to and from a remote server

Understanding sockets To communicate across the web, two programs must establish a connection, normally using TCP/IP, across which they send and receive messages. Think of a socket as an end point to the connection. Developers use the term sockets to describe the end points and the communication channel. This chapter examines the use of the WebSocket API to open, use, and later close a communication channel.

Testing Browsers for WebSocket Support <!DOCTYPE html> <html> <head> <script> function testSocketSupport() { if (window.WebSocket) alert("Sockets Supported"); else alert("Sockets Not Supported"); } </script> </head> <body onload="testSocketSupport()"> </body> </html>

Exchanging Messages With a WebSocket Server To perform server-based WebSocket communication, you need a server application that supports such communication. Across the Web, you can find webSocket servers written in PHP, Python, Java, and more. Several of the servers let you run them on your own localhost. To make it easy for you to get started, the Websocket.org website provides an “echo server,” which echos back messages it receives from a client. The following HTML file, SocketEcho.html, connects to the server and sends a text message. When the server receives and echos the message back, the webpage will display the received message within an alert dialog box.

Socketecho.html <!DOCTYPE html> <html> <head> <script> varwebsocket; function testSocketSupport() { if (window.WebSocket) { varServerURL = "ws://echo.websocket.org/"; websocket = new WebSocket(ServerURL); websocket.onopen = function(evt) { openFunction(evt) }; websocket.onclose = function(evt) { onClose(evt) }; websocket.onmessage = function(evt) { onMessage(evt) }; websocket.onerror = function(evt) { onError(evt) }; } else alert("Sockets Not Supported"); } function openFunction(event) { sendMessage("Test message"); } function onMessage(event) { alert("Message Received: " + event.data); }

continued function onClose(event) { alert(event.data); } function onError(event) { alert(event.data); } function sendMessage(msg) { websocket.send(msg); } </script> </head> <body onload="testSocketSupport();"> </body> </html>

A second example The following HTML file, AskMessage.html, prompts you for a message. When you click the Send button, the page uses a WebSocket to send the message to the server. The page then displays the message it receives back within the form.

Askmessage <!DOCTYPE html> <html> <head> <script> varwebsocket; varsocketsSupported = false; function Initialize() { if (window.WebSocket) { varServerURL = "ws://echo.websocket.org/"; websocket = new WebSocket(ServerURL); websocket.onopen = function(evt) { openFunction(evt) }; websocket.onclose = function(evt) { onClose(evt) }; websocket.onmessage = function(evt) { onMessage(evt) }; websocket.onerror = function(evt) { onError(evt) }; alert('Establishing Connection'); while (websocket.readyState == websocket.CONNECTING) ; // wait until socket is connected socketsSupported = true; } else alert("Sockets Not Supported"); }

continued function Send(msg) { Initialize(); if (socketsSupported) websocket.send(msg); } function onMessage(event) { document.getElementById('response').innerHTML = event.data; } function onClose(event) { alert(event.data); } function onError(event) { alert(event.data); } </script> </head> <body> Message: <input id="msg" type="text"></input> <button onclick="Send(document.getElementById('msg').value)">Send Message</button> <br/><br/> Response: <div id="response"></div> </body> </html>

Real world design – web socket api

summary As you surf the Web, you undoubtedly have seen sites that let you “chat with a representative” in real time using a text-based chat. To make it easier for developers to integrate such capabilities into the sites they create, HTML 5 provides a WebSocket API. This chapter introduces the API and how you can use it to communicate with a remote server. Over time, we can expect the API to continue to provide new capabilities, such as peer-to-peer communication.