package basic;
/*
 * socket.io-java-client Test.java
 *
 * Copyright (c) 2012, Enno Boland
 * socket.io-java-client is a implementation of the socket.io protocol in Java.
 * 
 * See LICENSE file for more information
 */
import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.Exception;
import java.lang.Object;
import java.lang.System;

public class BasicExample implements IOCallback {
	private SocketIO socket;

    /* 从 yunba.io 获取应用的 appkey */
    private static String APPKEY = "52fcc04c4dc903d66d6f8f92";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			new BasicExample();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public BasicExample() throws Exception {
		socket = new SocketIO();
		socket.connect("http://sock.yunba.io:3000/", this);
	}

	@Override
	public void onMessage(JSONObject json, IOAcknowledge ack) {
		try {
			System.out.println("Server said:" + json.toString(2));
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onMessage(String data, IOAcknowledge ack) {
		System.out.println("Server said: " + data);
	}

	@Override
	public void onError(SocketIOException socketIOException) {
		System.out.println("an Error occured");
		socketIOException.printStackTrace();
	}

	@Override
	public void onDisconnect() {
		System.out.println("Connection terminated.");
	}

	@Override
	public void onConnect() {
		System.out.println("Connection established");
	}

	@Override
	public void on(String event, IOAcknowledge ack, Object... args) {
		System.out.println("Server triggered event '" + event + "'");

        try {
            if (event.equals("socketconnectack")) {
                onSocketConnectAck();
            } else if (event.equals("connack")) {
                onConnAck((JSONObject)args[0]);
            } else if (event.equals("puback")) {
                onPubAck((JSONObject)args[0]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
	}

	public void onSocketConnectAck() throws Exception {
		System.out.println("onSocketConnectAck");

		// emit connect
        socket.emit("connect", new JSONObject("{'appkey': '" + APPKEY + "'}"));
	}

    public void onConnAck(JSONObject json) throws Exception {
        System.out.println("onConnAck success " + json.get("success"));

        socket.emit("publish", new JSONObject("{'topic': 't1', 'msg': 'hello form java socket.io client', 'qos': 1}"));
    }

    public void onPubAck(JSONObject json) throws Exception {
        System.out.println("conPubAck success " + json.get("success") + " msg id " + json.get("messageId"));
    }
}

@@ -475,16 +475,156 @@
 .System;
+%0Aimport java.util.List;%0Aimport java.nio.charset.Charset;%0Aimport java.io.PrintWriter;%0Aimport java.io.IOException;%0Aimport java.nio.file.Paths;
 %0A%0Apublic
@@ -2365,18 +2365,330 @@
 Ack%22);%0A%0A
-%09%09
+        try %7B%0A            List%3CString%3E file = java.nio.file.Files.readAllLines(Paths.get(%22sessionid.dat%22), Charset.defaultCharset());%0A            String sessionId = file.get(0);%0A            socket.emit(%22connect%22, new JSONObject(%22%7B'sessionid': '%22 + sessionId + %22'%7D%22));%0A        %7D catch (IOException e) %7B%0A            
 // emit 
@@ -2695,16 +2695,20 @@
 connect%0A
+    
         
@@ -2779,16 +2779,26 @@
 %22'%7D%22));%0A
+        %7D%0A
 %09%7D%0A%0A    
@@ -2924,24 +2924,375 @@
 uccess%22));%0A%0A
+        if (json.getBoolean(%22success%22)) %7B%0A            try %7B%0A                String sessionId = json.getString(%22sessionid%22);%0A                PrintWriter writer = new PrintWriter(%22sessionid.dat%22, Charset.defaultCharset().name());%0A                writer.println(sessionId);%0A                writer.close();%0A            %7D catch (Exception e) %7B%7D%0A        %7D%0A%0A
         sock

support sessionid when connect