-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeJs.lua
More file actions
67 lines (55 loc) · 2.46 KB
/
Copy pathNodeJs.lua
File metadata and controls
67 lines (55 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
--在服务器文件夹进入Dos终端窗口,输入npm install ws
--服务器代码,后缀是.js
var WebSocketServer = require('ws').Server;--Server引入WebSocket模块
console.log('Server on port 3000.');
var wss = new WebSocketServer({port: 3000});--监听3000窗口
--wss是是WebSocketServer对象,ws是WebSocket对象
--wss.on是服务端与客户端建立连接时触发的
wss.on('connection', function(ws) {
ws.send('Hello Cocos2d-x Lua');--触发客户端message事件,客户端是cc.WEBSOCKET_MESSAGE事件
ws.on('message', function (data){--注册接收客户端消息,如果客户端发消息,会在服务端显示。
console.log(data);
});
});
--open连接打开事件
--message接收消息事件
--close连接关闭事件
--error错误发生事件
--客户端代码,后缀是.lua
local function OnClickMenu1(menuItemSender)
if cc.WEBSOCKET_STATE_OPEN == wsSendText:getReadyState() then--判断是否是打开状态
cclog("Send Text WS is waiting...")
wsSendText:sendString("Hello WebSocket")
else
local warningStr = "WebSocket实例还没有准备好!"
cclog(warningStr)
end
end
local pItmLabel1 = cc.Label:createWithBMFont("fonts/fnt8.fnt", "Send Message")
local pItmMenu1 = cc.MenuItemLabel:create(pItmLabel1)
pItmMenu1:registerScriptTapHandler(OnClickMenu1)
local mn = cc.Menu:create(pItmMenu1)
mn:alignItemsVertically()
layer:addChild(mn)
--/////////////////// WebSocket start /////////////////
--ws是WebSocket协议,主机IP是127.0.0.1,端口是3000
wsSendText = cc.WebSocket:create("ws://127.0.0.1:3000")--实例化wsSendText对象
local function wsSendTextOpen(strData)
cclog("WebSocket实例打开")
end
local function wsSendTextMessage(strData)
local strInfo= "response text msg: "..strData
cclog(strInfo)
end
local function wsSendTextClose(strData)
cclog("WebSocket实例关闭")
end
local function wsSendTextError(strData)
cclog("WebSocket错误发生")
end
--监听WebSocket的各种事件
wsSendText:registerScriptHandler(wsSendTextOpen,cc.WEBSOCKET_OPEN)
wsSendText:registerScriptHandler(wsSendTextMessage,cc.WEBSOCKET_MESSAGE)
wsSendText:registerScriptHandler(wsSendTextClose,cc.WEBSOCKET_CLOSE)
wsSendText:registerScriptHandler(wsSendTextError,cc.WEBSOCKET_ERROR)
--/////////////////// WebSocket end /////////////////