使用cocoscreator + node.js + websocket实现简单的聊天服务

先上个效果图:

使用cocoscreator + node.js + websocket实现简单的聊天服务

使用cocoscreator 1.9.1 + node.js + websocket实现,没有使用socket.io, 全部自己封装,长连接进行封装后可以和短连接使用方法一样,使用简单,方便以后开发网络游戏。

1、客户端:

  主要就是聊天内容的显示,自动换行和背景扩展,代码大概如下:

cc.Class({ extends: cc.Component, properties: { msgLabel: cc.Label, uidLabel: cc.Label, msgLayout: cc.Layout, msgBg: cc.Node, maxLen: 500, }, // LIFE-CYCLE CALLBACKS: // onLoad () {}, start () { this.node.runAction(cc.fadeTo(0.5, 255)) }, initMsg(msg, uid){ this.msgLabel.string = msg; this.uidLabel.string = uid; this.msgLabel.overflow = cc.Label.Overflow.NONE; // this.msgBg.width = this.msgLabel.node.width + 10; // this.msgBg.height = this.msgLabel.node.height + 10; // this.node.height = this.msgBg.height + 40; this.scheduleOnce((dt)=>{ if ( this.msgLabel.node.width >= this.maxLen){ this.msgLabel.overflow = cc.Label.Overflow.RESIZE_HEIGHT; this.msgLabel.node.width = this.maxLen; } this.msgBg.width = this.msgLabel.node.width + 10; this.msgBg.height = this.msgLabel.node.height + 10; this.node.height = this.msgBg.height + 40; }, 0); this.node.opacity = 0; } // update (dt) {}, });

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpggsy.html