140 likes | 246 Views
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);
E N D
Pro HTML5 ProgrammingPowerful APIs for Richer Internet Application Development
overview • Sockets are a protocol independent method of creating a connection between processes.
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.
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
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) { }
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) { }
JAVA WEB SOCKET SEVER • It is based on sokets. • Java Websocket Solution • http://java-websocket.org/ • http://jwebsocket.org/
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) }
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);}
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
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);
1.連線對戰遊戲配對控制 • 2.身份辨識 • 3.亂數子的產生 • 4.同步