1 / 9

Web Sockets

Web Sockets. CSCI 201 Principles of Software Development. Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu. Outline. Web Sockets Program. Web Sockets. WebSockets were standardized by the IETF in 2011

vrabel
Download Presentation

Web Sockets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web Sockets CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu

  2. USC CSCI 201L Outline • Web Sockets • Program

  3. Web Sockets • WebSockets were standardized by the IETF in 2011 • WebSockets provide a means for persistent two-way communication between a web browser and a web server • Before WebSockets, all communication with the server had to be initiated by the user, such as by clicking on a link • Having a persistent connection allows the client or the server to initiate communication once the connection is established USC CSCI 201L

  4. Web Socket Client Code Description • There are a number of different implementations of WebSockets • We will use the JavaScript WebSocket class for our client-side code • There are four event functions you can override onopen – called when socket connection is established onmessage – called with client receives data from server onerror – called when any error occurs in communication onclose – called when the connection is closed • The constructor to the WebSocket takes a URI as a parameter, specifying the location of the WebSocket endpoint • The send function allows you to send data to the server USC CSCI 201L

  5. Web Socket Client Code 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Chat Client</title> 5 <script> 6 var socket; 7 function connectToServer() { 8 socket = new WebSocket("ws://localhost:8080/Test-Web/ws"); 9 socket.onopen = function(event) { 10 document.getElementById("mychat").innerHTML += "Connected!"; 11 } 12 socket.onmessage = function(event) { 13 document.getElementById("mychat").innerHTML += event.data + "<br />"; 14 } 15 socket.onclose = function(event) { 16 document.getElementById("mychat").innerHTML += "Disconnected!"; 17 } 18 } 19 function sendMessage() { 20 socket.send("Jeff: " + document.chatform.message.value); 21 return false; 22 } 23 </script> 24 </head> 25 <body onload="connectToServer()"> 26 <form name="chatform" onsubmit="return sendMessage();"> 27 <input type="text" name="message" value="Type Here!" /><br /> 28 <input type="submit" name="submit" value="Send Message" /> 29 </form> 30 <br /> 31 <div id="mychat"></div> 32 </body> 33 </html> USC CSCI 201L

  6. Web Socket Server Code Description • There are a number of different server-side implementations of WebSockets • We will be using Java as our WebSocket endpoint language • The @ServerEndpoint annotation can be used to specify the name of the WebSocket endpoint @ServerEndpoint (value=“/ws”) • There are four methods that can be overridden then, which can be done with annotations as well (see next slide) OnOpen(javax.websocket.Session) OnMessage(String, javax.websocket.Session) OnClose(javax.websocket.Session) OnError(Throwable) USC CSCI 201L

  7. Web Socket Server Code 1 package csci201; 2 import java.io.IOException; 3 import java.util.Vector; 4 import javax.websocket.*; // for space 5 import javax.websocket.server.ServerEndpoint; 6 7 @ServerEndpoint(value = "/ws") 8 public class ServerSocket { 9 private static Vector<Session> sessionVector = new Vector<Session>(); 10 @OnOpen 11 public void open(Session session) { 12 System.out.println("Connection made!"); 13 sessionVector.add(session); 14 } 15 @OnMessage 16 public void onMessage(String message, Session session) { 17 System.out.println(message); 18 try { 19 for(Session s : sessionVector) { 20 s.getBasicRemote().sendText(message); 21 } 22 } catch (IOExceptionioe) { 23 System.out.println("ioe: " + ioe.getMessage()); 24 close(session); 25 } 26 } 27 @OnClose 28 public void close(Session session) { 29 System.out.println("Disconnecting!"); 30 sessionVector.remove(session); 31 } 32 @OnError 33 publicvoid error(Throwable error) { 34 System.out.println("Error!"); 35 } 36 } USC CSCI 201L

  8. USC CSCI 201L Outline • Web Sockets • Program

  9. Program Program • Add a web client to the chat application written in the previous class so there can be communication between a web client and a standalone application. C:>java ChatClient localhost 6789 Hello, how are you? Them: Fine, and you? Good, thanks. USC CSCI 201L

More Related