//
//  TCPClient.swift
//  SocketChat
//
//  Created by Justine on 16/5/27.
//  Copyright © 2016年 AppCoda. All rights reserved.
//

import Foundation
import CocoaAsyncSocket
import SocketIOClientSwift

protocol SocketIODelegate {
    //   didsend()
    //   didrecived()
}
class TCPClient{
    static let shareInstance = TCPClient()
    var counter = 0
    var socket:SocketIOClient?
    func connect(urlstr:String){
        socket = SocketIOClient.init(socketURL: NSURL(string: urlstr)!)
        socket?.connect()
        //socket = SocketIOClient.ini
        print(socket?.status)
    }
    
    func disconnect(){
        socket?.disconnect()
    }
    func reconnect(){
        socket?.reconnect()
    }
    //发送消息
    func send(event:String,data:AnyObject){
        socket?.emit(event, data)
    }
    
    
    //接收消息
    func recived(event:String){
        socket?.on(event, callback: { (data, Ack) in
            switch (event){
            case "zhuxunserver.response": break
                //收到消息后具体处理
                
            case "zhuxunserver.chat.message":break
                //收到消息后具体处理
                
            case "zhuxunserver.users.state":break
                //收到消息后具体处理
                
            case "zhuxunserver.login.locked":break
                //收到消息后具体处理
            case "zhuxunserver.chatmessage.sync":break
                
            default:
                print("Unrecognized event")
            }
            Ack.with(event)
        })
    }
    
    //        switch event {
    //        case "zhuxunserver.response":
    //            socket?.on(event, callback: { (data, Ack) in
    //
    //                //Ack("clientRcomplet")
    //            })
    //        case "zhuxunserver.chat.message":
    //            socket?.on(event, callback: { (data, Ack) in
    //
    //                //Ack("clientRcomplet")
    //            })
    //        case "zhuxunserver.users.state":
    //            socket?.on(event, callback: { (data, Ack) in
    //
    //                //Ack("clientRcomplet")
    //            })
    //        case "zhuxunserver.login.locked":
    //            socket?.on(event, callback: { (data, Ack) in
    //
    //            })
    //        default:
    //            print("###################################################")
    //            print("###################################################")
    //            print("#####socket recived Unrecognize event##############")
    //            print("###################################################")
    //            print("###################################################")
    //        }
    
 
}