coco2dx客户端聊天系统(普通版)

客户端:

ui文件夹下的chat_layer.lua:

local layer         = import( 'world.layer')

chat_layer = lua_class('chat_layer', layer.layer)

local msg = ""

function chat_layer:_init()
	super(chat_layer, self)._init()
	self:create_chat_line()
	self:create_send_button()
	self:create_chat_box()
end

function chat_layer:create_chat_line()
	
	function ChatLine_EventHandle(strEventName,pSender)
		local edit = pSender
		if strEventName == "ended" or "changed" then
			msg = string.format( "%s", edit:getText())
		end
	end

	self.chat_1 = cc.EditBox:create( cc.size(300,50), cc.Scale9Sprite:create('gui/pattern1.png'))
	self.chat_1:setPosition(800,530)
	self.chat_1:setOpacity(100)
	self.chat_1:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE )
	--Handler
	self.chat_1:registerScriptEditBoxHandler(ChatLine_EventHandle)
	self.cc:addChild(self.chat_1)
end

function chat_layer:create_chat_box(msg)
	chat_3 = cc.EditBox:create( cc.size(300,300), cc.Scale9Sprite:create('gui/pattern3.png'))
	chat_3:setPosition(800,130)
	chat_3:setOpacity(100)
	local targetPlatform = cc.Application:getInstance():getTargetPlatform()
    if kTargetIphone == targetPlatform or kTargetIpad == targetPlatform then
	   chat_3:setFontName("msyh")
	else
		chat_3:setFontName("fonts/msyh.ttf")
	end
	chat_3:setFontSize(20)
	chat_3:setInputMode(ANY)
	self.cc:addChild(chat_3)
	chat_3:setEnabled(false)
end

function chat_layer:create_send_button()
	
	function SendButton_EventHandle(sender,eventType)
    	if eventType == ccui.TouchEventType.ended then
    		--self.chat_3:setText(msg)
    		server.say(msg)
    	end
	end
	
	self.chat_2 = ccui.Button:create()
	self.chat_2:setTouchEnabled(true)
	self.chat_2:setPosition(1000,530)
	self.chat_2:loadTextures( 'gui/weapon_5.png' , 'gui/weapon_5.png' , '' )
	self.chat_2:setTag(8)
	self.cc:addChild(self.chat_2)
	--Handler
	self.chat_2:addTouchEventListener(SendButton_EventHandle)
end

function chat_layer:chat_msg(recv_msg)
	chat_3:setText(recv_msg)
end

entities文件夹下的avatar:

proxy = lua_class( 'proxy' )
local chat_layer        = import('ui.chat_layer')
function proxy:_init()
end
function proxy:chat_msg(msg)
	chat_layer.chat_layer:chat_msg(msg)
end

服务端:

entities文件夹下的avatar.lua:

avatar = lua_class('avatar')
function avatar:_init(pid)
	self.pid = pid
end
function avatar:say(msg)
	for i, e in pairs(entities) do
		e.client.chat_msg(msg)
	end
end


你可能感兴趣的:(客户端聊天系统)