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;
import java.util.List;
import java.nio.charset.Charset;
import java.io.PrintWriter;
import java.io.IOException;
import java.nio.file.Paths;

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

        try {
            List<String> file = java.nio.file.Files.readAllLines(Paths.get("sessionid.dat"), Charset.defaultCharset());
            String sessionId = file.get(0);
            socket.emit("connect", new JSONObject("{'sessionid': '" + sessionId + "'}"));
        } catch (IOException e) {
            // emit connect
            socket.emit("connect", new JSONObject("{'appkey': '" + APPKEY + "'}"));
        }
	}

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

        if (json.getBoolean("success")) {
            try {
                String sessionId = json.getString("sessionid");
                PrintWriter writer = new PrintWriter("sessionid.dat", Charset.defaultCharset().name());
                writer.println(sessionId);
                writer.close();
            } catch (Exception e) {}
        }

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

@@ -615,16 +615,39 @@
 e.Paths;
+%0Aimport java.util.UUID;
 %0A%0Apublic
@@ -2384,24 +2384,54 @@
 nectAck%22);%0A%0A
+        String customId = %22%22;%0A
         try 
@@ -2508,23 +2508,22 @@
 hs.get(%22
-session
+custom
 id.dat%22)
@@ -2559,38 +2559,30 @@
             
-String session
+custom
 Id = file.ge
@@ -2599,98 +2599,45 @@
     
-    socket.emit(%22connect%22, new JSONObject(%22%7B'sessionid': '%22 + sessionId + %22'%7D%22));%0A
+%7D catch (IOException e) %7B%0A   
         
 %7D ca
@@ -2636,38 +2636,61 @@
     
-%7D
  c
-atch (IOException e) %7B%0A
+ustomId = UUID.randomUUID().toString();%0A    
     
+%7D%0A%0A
     
@@ -2709,20 +2709,16 @@
 connect%0A
-    
         
@@ -2787,300 +2787,70 @@
 + %22'
-%7D%22));%0A        %7D%0A%09%7D%0A%0A    public void onConnAck(JSONObject json) throws Exception %7B%0A        System.out.println(%22onConnAck success %22 + json.get(%22success%22));%0A%0A        if (json.getBoolean(%22success%22)) %7B%0A            try %7B%0A                String sessionId = json.getString(%22sessionid%22);%0A             
+, 'customid': '%22 + customId + %22'%7D%22));%0A%0A        try %7B%0A        %09
    P
@@ -2886,23 +2886,22 @@
 Writer(%22
-session
+custom
 id.dat%22,
@@ -2943,28 +2943,24 @@
             
-    
 writer.print
@@ -2966,24 +2966,19 @@
 tln(
-session
+custom
 Id);%0A
-    
     
@@ -3005,28 +3005,24 @@
 ();%0A        
-    
 %7D catch (Exc
@@ -3034,25 +3034,153 @@
 n e) %7B%7D%0A
-        %7D
+%09%7D%0A%0A    public void onConnAck(JSONObject json) throws Exception %7B%0A        System.out.println(%22onConnAck success %22 + json.get(%22success%22));
 %0A%0A      

use customid