pro html5 programming powerful apis for richer internet application development n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Pro HTML5 Programming Powerful APIs for Richer Internet Application Development PowerPoint Presentation
Download Presentation
Pro HTML5 Programming Powerful APIs for Richer Internet Application Development

Loading in 2 Seconds...

play fullscreen
1 / 14

Pro HTML5 Programming Powerful APIs for Richer Internet Application Development - PowerPoint PPT Presentation


  • 87 Views
  • Uploaded on

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);

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Pro HTML5 Programming Powerful APIs for Richer Internet Application Development' - hei


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
overview
overview
  • Sockets are a protocol independent method of creating a connection between processes.
java socket api
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 api1
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
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
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
JAVA WEB SOCKET SEVER
  • It is based on sokets.
  • Java Websocket Solution
      • http://java-websocket.org/
    • http://jwebsocket.org/
java web socket api
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
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
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 websocket chatroom
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);
slide14

1.連線對戰遊戲配對控制

  • 2.身份辨識
  • 3.亂數子的產生
  • 4.同步