1 / 14

Pro HTML5 Programming Powerful APIs for Richer Internet Application Development

Pro HTML5 Programming Powerful APIs for Richer Internet Application Development. Chap 10. Socket programming API. overview. Sockets are a protocol independent method of creating a connection between processes. JAVA Socket API. java.net.Socket Socket( InetAddress addr , int port);

hei
Download Presentation

Pro HTML5 Programming Powerful APIs for Richer Internet Application Development

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. Pro HTML5 ProgrammingPowerful APIs for Richer Internet Application Development

  2. Chap 10. Socket programmingAPI

  3. overview • Sockets are a protocol independent method of creating a connection between processes.

  4. JAVA Socket API • java.net.Socket • Socket(InetAddressaddr, int port); • create a Socket connection to address addr on port port • InputStreamgetInputStream(); • returns an instance of InputStream for getting info from the implicit Socket object • OutputStreamgetOutputStream(); • returns an instance of OutputStream for sending info to implicit Socket object. • close(); • close connection to implicit socket object, cleaning up resources.

  5. JAVA Socket API • java.net.ServerSocket • ServerSocket(intport); • enables program to listen for connections on port port • Socket accept(); • blocks until connection is requested via Socket request from some other process. When connection is established, an instance of Socket is returned for establishing communication streams. • java.net.InetAddress • static InetAddressgetByName(String name) • given a hostname name, return the InetAddress object representing that name (basically encapsulates name and IP associated with name); • static InetAddress[] getAllByName(String name) • same as above but for case where many ip’s mapped to single name (try www.microsoft.com, e.g.). • static InetAddressgetLocalHost() • get InetAddress object associated with local host. • static InetAddressgetByAddress(byte[] addr) • get InetAddress object associated with address addr

  6. Simple server try { serverSocket= new ServerSocket(port); clientSocket= serverSocket.accept(); out = new DataOutputStream( clientSocket.getOutputStream()); String str = “HELLO”; out.write(str.getBytes(), 0, str.getBytes(.length); out.flush(); } catch (IOException ex) { }

  7. Simple Client try { clientSocket= new Socket(port); in = new DataInputStream(clientSocket.getInputStream()); inByte = new byte[1024]; in.read(inByte); server = new String(inByte, 0, inByte.length); System.out.println(server.trim()); } catch (IOException ex) { }

  8. JAVA WEB SOCKET SEVER • It is based on sokets. • Java Websocket Solution • http://java-websocket.org/ • http://jwebsocket.org/

  9. JAVA WEB SOCKET API • Import WebSocket Library • http://java-websocket.org/ import org.java_websocket.server.WebSocketServer; import org.java_websocket.WebSocket; public class ChatServer extends WebSocketServer implements ActionListener { private WebSocket player; public ChatServer(int port) { super(port); } public void onClientOpen(WebSocket conn) { try { this.player= conn; this.player.send("Hello Player"); // sendToAll } catch (Exception ex) { ex.printStackTrace();} } public void onClientClose(WebSocket conn) public void onClientMessage(WebSocket conn, String message) public void onError(Throwable ex) }

  10. WEB socket • Creating a WebSocket • url= "ws://localhost:8887"; • w = new WebSocket(url); • Add Event Listeners • w.onopen = function() { log("open");w.send("thank you for accepting this websocket request");} • w.onmessage = function(e) { log(e.data);} • w.onclose = function(e) { log("closed");} • Sending Message • document.getElementById("sendButton").onclick = function() {w.send(document.getElementById("inputMessage").value);}

  11. WebSocket Server • A Simple Java WebSocket Server • https://github.com/TooTallNate/Java-WebSocket • Compile WebSocket Server Example • ant • Run WebSocket Server • java -cp build/examples:dist/java_websocket.jar ChatServer

  12. A websocketchatroom • function connectServer() { • varurl= "ws://localhost:8887"; • socket = new WebSocket(url); • socket.onopen = function() { • updateSocketStatus("Connected to WebSocket server"); • } • socket.onmessage = function(e) { • updateSocketStatus("Update message: " + e.data); • } • } • function sendMessage() { • var message = document.getElementById("sendMessage").value; • socket.send(message); • } • document.getElementById("connectButton").addEventListener("click", connectServer); • document.getElementById("sendMsgButton").addEventListener("click", sendMessage);

  13. A websocketchatroom

  14. 1.連線對戰遊戲配對控制 • 2.身份辨識 • 3.亂數子的產生 • 4.同步

More Related