刚学习了lua两三天,对socket通信做了个自己的封装,导入全局实例,加入自定义事件分发,请大家多多指教
-- -- Author: peilin -- Date: 2015-01-22 11:10:35 -- local Component = require("framework.cc.components.Component") local SocketTCP = require("framework.cc.net.SocketTCP") local SERVER_IP = "127.0.0.1" local SERVER_PORT = 8888 local OlafSocket = class("OlafSocket",Component) --OLAF EVENT OlafSocket.EVENT_CLOSE = "SOCKET_TCP_CLOSE" OlafSocket.EVENT_CLOSED = "SOCKET_TCP_CLOSED" OlafSocket.EVENT_CONNECTED = "SOCKET_TCP_CONNECTED" OlafSocket.EVENT_CONNECT_FAILURE = "SOCKET_TCP_CONNECT_FAILURE" OlafSocket.EVENT_OLAF_DATA = "OLAF_DATA" function OlafSocket:ctor(__name,...) self.name = __name or 'OlafSocket' self.host = SERVER_IP self.port = SERVER_PORT self.isRetryConnect = false self.isConnected = false self.listener = {} self.nextListenerID = 0 self.socket = SocketTCP.new(self.host,self.port,self.isRetryConnect) self.socket:addEventListener(SocketTCP.EVENT_CONNECTED,handler(self,self.onStatus)) self.socket:addEventListener(SocketTCP.EVENT_CLOSE,self.onStatus)) self.socket:addEventListener(SocketTCP.EVENT_CLOSED,self.onStatus)) self.socket:addEventListener(SocketTCP.EVENT_CONNECT_FAILURE,self.onStatus)) self.socket:addEventListener(SocketTCP.EVENT_DATA,self.onData)) end function OlafSocket:connect(obj) if 'table' ~= type(obj) then printError("OlafSocket:connect require parameter 1 to be a table") return nil end assert(obj.socket,"Invalid socket object") obj.socket:connect() return true end function OlafSocket:disConnect( obj ) if 'table' ~= type(obj) then printError("OlafSocket:connect require parameter 1 to be a table") return nil end if not obj.socket then return nil end print("disconnect()") obj.socket:disconnect() return true end function OlafSocket:dispatchEvent(__event) if 'table' ~= type(__event) then printError("OlafSocket:dispatchEvent() - invalid param") return nil end if self.listener[__event.name] == nil then if DEBUG > 1 then printInfo("event name :%s no listener.",__event.name) end return true end for id,listener in pairs(self.listener[__event.name]) do -- listener[1] = handler() -- listener[2] = tag ret = listener[1](__event) if __event.stop == true and ret == true then break end end return true end function OlafSocket:onStatus(__status) if __status.name == SocketTCP.EVENT_CONNECTED then self.isConnected = true end if __status.name == SocketTCP.EVENT_CLOSED then self.isConnected = false end ret = self:dispatchEvent({name = __status.name,stop = true}) return ret end function OlafSocket:onData( __data ) ret = self:dispatchEvent({name = OlafSocket.EVENT_OLAF_DATA,stop = true,data = __data.data}) return ret end function OlafSocket:addListener(obj,__eventName,__listener,tag) if 'table' ~= type(obj) then printError("OlafSocket:addListener require parameter 1 to be a table") return nil end assert(type(__eventName) == "string" and __eventName ~= "","OlafSocket:addListener() - require parameter 2 to be a string") if obj.listener[__eventName] == nil then obj.listener[__eventName] = {} end -- listener[1] = handler() -- listener[2] = tag tag = tag or "" obj.nextListenerID = obj.nextListenerID + 1 obj.listener[__eventName][obj.nextListenerID] = {__listener,tag} return true end function OlafSocket:removeListeners(obj,__listener) if 'table' ~= type(obj) then printError("OlafSocket:sendData require parameter 1 to be a table") return nil end for event,handlers in pairs(obj.listener) do for listenerID,listener in pairs(handlers) do if listener[1] == __listener then handlers[listenerID] = nil end end end return true end function OlafSocket:removeListenersByTag(obj,__tagToRemove) if 'table' ~= type(obj) then printError("OlafSocket:sendData require parameter 1 to be a table") return nil end for event,listener in pairs(handlers) do if listener[2] == __tagToRemove then handlers[listenerID] = nil end end end return true end function OlafSocket:removeListenersByEvent(obj,__eventToRemove) if 'table' ~= type(obj) then printError("OlafSocket:sendData require parameter 1 to be a table") return nil end obj.listener[__eventToRemove] = nil return true end function OlafSocket:removeAllListener( obj ) if 'table' ~= type(obj) then printError("OlafSocket:sendData require parameter 1 to be a table") return nil end obj.listener = nil end function OlafSocket:sendData(obj,__data) if 'table' ~= type(obj) then printError("OlafSocket:sendData require parameter 1 to be a table") return nil end if DEBUG > 1 then printInfo("Send Data") for k,v in pairs(__data) do print(k,v) end end if obj.isConnected == false then printError("OlafSocket:sendData require connect first") return nil end obj.socket:send(__data) return true end -- register global _G OlafSocket.global = OlafSocket.new("OlafSocket_G") function OlafSocket.global:connect() return OlafSocket:connect(self) end function OlafSocket.global:disConnect() return OlafSocket:disConnect(self) end function OlafSocket.global:sendData(__data) return OlafSocket:sendData(self,__data) end function OlafSocket.global:addListener(__eventName,__listener) return OlafSocket:addListener(self,__listener) end function OlafSocket.global:removeListeners( __listener ) return OlafSocket:removeListeners(self,__listener) end function OlafSocket.global:removeListenersByTag(__tagToRemove) return OlafSocket:removeListenersByTag(self,__tagToRemove) end function OlafSocket.global:removeListenersByEvent( __event ) return OlafSocket:removeListenersByEvent(self,__event) end function OlafSocket.global:removeAllListener() return OlafSocket:removeAllListener(self) end _G.OlafNet = _G.OlafNet or {} _G.OlafNet.OlafSocket = OlafSocket return OlafSocket原文链接:https://www.f2er.com/cocos2dx/344716.html