客户端代码html
// websocket.js 导出的本身封装的websocket模块 var websocket = { sock: null, // 链接的socket 对象 WebSocket, h5标准对象; // 网络链接成功了之后调用 on_open: function(event) { // test this.send_data("HelloWorld"); // end }, // 客户端收到数据的时候 on_message: function(event) { console.log("#####", event.data); }, // 客户端收到socket 关闭的时间的时候调用; on_close: function (event) { this.close(); }, on_error: function (event) { this.close(); }, close: function() { if (this.sock != null) { this.sock.close(); // 关闭socket this.sock = null; } }, // 链接函数, connect: function(url) { this.sock = new WebSocket(url); // h5标准的websocket对象 this.sock.binaryType = "arraybuffer"; // 配置接受二进制的数据类型,与服务器保持一次, "Blob" // 为这个websocket对象制定对应的回掉函数; this.sock.onopen = this.on_open.bind(this); this.sock.onmessage = this.on_message.bind(this); this.sock.onclose = this.on_close.bind(this); this.sock.onerror = this.on_error.bind(this); }, // 发送数据, sock.send; send_data: function(data) { this.sock.send(data); }, }; module.exports = websocket;
// game_scene.js 使用webscoket和socket.io的案例 var websocket = require("websocket"); var net = require("net"); cc.Class({ extends: cc.Component, properties: { // foo: { // default: null, // The default value will be used only when the component attaching // to a node for the first time // url: cc.Texture2D, // optional, default is typeof default // serializable: true, // optional, default is true // visible: true, // optional, default is true // displayName: 'Foo', // optional // readonly: false, // optional, default is false // }, // ... }, // use this for initialization onLoad: function () { // net.connect("127.0.0.1:6081"); websocket.connect("ws://127.0.0.1:6080/ws"); }, // called every frame, uncomment this function to activate update callback // update: function (dt) { // }, });
服务器代码node
// echo_server.js websocket服务器代码 var ws = require("ws"); var server = new ws.Server({ // host: ip, // 若是加了host,外部连接不上 port: 6080, }); console.log("#######"); function on_server_client_comming(session) { session.on("close", function() { }); // error事件 session.on("error", function(err) { }); // end session.on("message", function(data) { console.log("######", data); session.send(data); }); } server.on("connection", on_server_client_comming); var socket_io = require('socket.io') var sio = socket_io(6081); sio.sockets.on('connection',function(socket){ console.log("connect called"); // 自定义事件 socket.on("your_echo", function (data) { console.log("your_echo", data); socket.emit("your_echo", data); }); // end // 系统事件 socket.on('disconnect',function(data){ console.log("disconnect"); }); });
客户端代码web
// 导出的net模块 net.js if(window.io == null){ // h5 window.io = require("socket-io"); } var net = { sio: null, connect:function(url) { var self = this; var opts = { 'reconnection':true, 'force new connection': true, 'transports':['websocket', 'polling'] } this.sio = window.io.connect(url, opts); // 监听地城的系统事件 this.sio.on('reconnect',function(){ console.log('reconnection'); }); // 链接成功 this.sio.on('connect',function(data){ self.sio.connected = true; console.log("%%%%%%%%%%%%% connect"); // 事件 + 数据名字 self.send("your_echo", "HelloWorld"); }); // 断开链接 this.sio.on('disconnect',function(data){ console.log("MMMMMdisconnect"); self.sio.connected = false; // self.close(); }); // 链接失败 this.sio.on('connect_failed',function (){ console.log('connect_failed'); }); // 本身定义,若是你向要收那种事件的数据,你就监听这个事件 this.sio.on('your_echo',function(data){ console.log("your_echo", data); }); }, // 发送数据: 事件+数据的模型; send:function(event,data){ if(this.sio.connected){ this.sio.emit(event,data); // 触发一个网络事件,名字 + 数据body ---> 服务器; } }, // 关闭socket close:function(){ if(this.sio && this.sio.connected){ this.sio.connected = false; this.sio.disconnect(); // API this.sio = null; } }, }; module.exports = net;
// socket-io.js H5使用的io库文件,自行下载,须要添加判断!CC_JSB && !cc.sys.isNative if (!CC_JSB && !cc.sys.isNative) { ... ... }
// game_scene.js 使用webscoket和socket.io的案例 var websocket = require("websocket"); var net = require("net"); cc.Class({ extends: cc.Component, properties: { // foo: { // default: null, // The default value will be used only when the component attaching // to a node for the first time // url: cc.Texture2D, // optional, default is typeof default // serializable: true, // optional, default is true // visible: true, // optional, default is true // displayName: 'Foo', // optional // readonly: false, // optional, default is false // }, // ... }, // use this for initialization onLoad: function () { net.connect("127.0.0.1:6081"); // websocket.connect("ws://127.0.0.1:6080/ws"); }, // called every frame, uncomment this function to activate update callback // update: function (dt) { // }, });
服务器代码服务器
// echo_server.js websocket服务器代码 var ws = require("ws"); var server = new ws.Server({ // host: ip, // 若是加了host,外部连接不上 port: 6080, }); console.log("#######"); function on_server_client_comming(session) { session.on("close", function() { }); // error事件 session.on("error", function(err) { }); // end session.on("message", function(data) { console.log("######", data); session.send(data); }); } server.on("connection", on_server_client_comming); // socketio var socket_io = require('socket.io') var sio = socket_io(6081); sio.sockets.on('connection',function(socket){ console.log("connect called"); // 自定义事件 socket.on("your_echo", function (data) { console.log("your_echo", data); socket.emit("your_echo", data); }); // end // 系统事件 socket.on('disconnect',function(data){ console.log("disconnect"); }); });