Web Sockets CSCI 201 Principles of Software Development
Web Sockets CSCI 201 Principles of Software Development Jeffrey Miller, Ph. D. jeffrey. miller@usc. edu
Outline • Web Sockets • Program USC CSCI 201 L
Web Sockets ▪ Web. Sockets were standardized by the IETF in 2011 ▪ Web. Sockets provide a means for persistent two-way communication between a web browser and a web server › Before Web. Sockets, 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 201 L 3/9
Web Socket Client Code Description ▪ There a number of different implementations of Web. Sockets ▪ We will use the Java. Script Web. Socket 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 Web. Socket takes a URI as a parameter, specifying the location of the Web. Socket endpoint ▪ The send function allows you to send data to the server USC CSCI 201 L 4/9
Web Socket Client Code 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Chat Client</title> 5 <script> 6 var socket; 7 function connect. To. Server() { 8 socket = new Web. Socket("ws: //localhost: 8080/Test-Web/ws"); 9 socket. onopen = function(event) { 10 document. get. Element. By. Id("mychat"). inner. HTML += "Connected!"; 11 } 12 socket. onmessage = function(event) { 13 document. get. Element. By. Id("mychat"). inner. HTML += event. data + " "; 14 } 15 socket. onclose = function(event) { 16 document. get. Element. By. Id("mychat"). inner. HTML += "Disconnected!"; 17 } 18 } 19 function send. Message() { 20 socket. send("Jeff: " + document. chatform. message. value); 21 return false; 22 } 23 </script> 24 </head> 25 <body onload="connect. To. Server()"> 26 <form name="chatform" onsubmit="return send. Message(); "> 27 <input type="text" name="message" value="Type Here!" /> 28 <input type="submit" name="submit" value="Send Message" /> 29 </form> 30 31 <div id="mychat"></div> 32 </body> 33 </html> USC CSCI 201 L 5/9
Web Socket Server Code Description ▪ There a number of different server-side implementations of Web. Sockets ▪ We will be using Java as our Web. Socket endpoint language ▪ The @Server. Endpoint annotation can be used to specify the name of the Web. Socket endpoint @Server. Endpoint (value=“/ws”) ▪ There are four methods that can be overridden then, which can be done with annotations as well (see next slide) On. Open(javax. websocket. Session) On. Message(String, javax. websocket. Session) On. Close(javax. websocket. Session) On. Error(Throwable) USC CSCI 201 L 6/9
Web Socket Server Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package csci 201; import java. io. IOException; import java. util. Vector; import javax. websocket. *; // for space import javax. websocket. server. Server. Endpoint; @Server. Endpoint(value = "/ws") public class Server. Socket { private static Vector<Session> session. Vector = new Vector<Session>(); @On. Open public void open(Session session) { System. out. println("Connection made!"); session. Vector. add(session); } @On. Message public void on. Message(String message, Session session) { System. out. println(message); try { for(Session s : session. Vector) { 27 @On. Close s. get. Basic. Remote(). send. Text(message); 28 public void close(Session session) { } 29 System. out. println("Disconnecting!"); } catch (IOException ioe) { 30 session. Vector. remove(session); System. out. println("ioe: " + ioe. get. Message()); 31 } close(session); 32 @On. Error } 33 public void error(Throwable error) { } 34 System. out. println("Error!"); 35 } 36 } USC CSCI 201 L 7/9
Outline • Web Sockets • Program USC CSCI 201 L
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 Chat. Client localhost 6789 Hello, how are you? Them: Fine, and you? Good, thanks. • Program USC CSCI 201 L 9/9
- Slides: 9