加入收藏 | 设为首页 | 会员中心 | 我要投稿 常州站长网 (https://www.0519zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

node中socket.io的事件使用详解

发布时间:2016-11-24 16:12:40 所属栏目:Linux 来源:站长网
导读:socket.io类库不但可以相互发送消息,而且还可以通过socket端口对象的emit方法互相发送事件. emit在之前的事件上说过现在一句话带过:emit是用来手动触发事件的. 复制代码 代码如下: socket.emit(event,data,function(data1,data2......){ }); 在使用emit方

socket.io类库不但可以相互发送消息,而且还可以通过socket端口对象的emit方法互相发送事件.

emit在之前的事件上说过现在一句话带过:emit是用来手动触发事件的.

复制代码 代码如下:
socket.emit(event,data,function(data1,data2......){
});

在使用emit方法发送事件时,可以再另一端使用socket端口对象的on方法会once方法监听.

复制代码 代码如下:
socket.on(event,function(data,fn){
});
socket.once(event,function(data,fn){
})

上面的回调函数中的参数data:对方发送的事件中携带的数据,

fn:对方在发送事件时指定的callback回调函数.

案例1:当服务器和客户端连接后,向客户端发送一个news事件,事件中携带一个对象,该对象的hello属性值为"你好".在接收到客户端发送my other event事件时,在控制台中输出"服务器端就收到数据"+客户端发送事件中携带的数据.

服务器端代码,server.js

复制代码 代码如下:
nbsp;var http=require("http");
nbsp;var sio=require("socket.io");
nbsp;var fs=require("fs");
nbsp;var server=http.createServer(function (req,res) {
nbsp;nbsp;nbsp;nbsp; res.writeHead(200,{"Content-type":"text/html"});
nbsp;nbsp;nbsp;nbsp; res.end(fs.readFileSync("./index.html"));
nbsp;});
nbsp;server.listen(1337);
nbsp;var socket=sio.listen(server);
nbsp;socket.on("connection", function (socket) {
nbsp;nbsp;nbsp;nbsp; socket.emit("news",{hello:"你好"});
nbsp;nbsp;nbsp;nbsp; socket.on("my other event", function (data) {
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; console.log("服务器端接受到信息%j",data);
nbsp;nbsp;nbsp;nbsp; });
nbsp;});

客户端index.html代码:

复制代码 代码如下:
nbsp;lt;!DOCTYPE htmlgt;
nbsp;lt;htmlgt;
nbsp;lt;head lang="en"gt;
nbsp;nbsp;nbsp;nbsp; lt;meta charset="UTF-8"gt;
nbsp;nbsp;nbsp;nbsp; lt;titlegt;lt;/titlegt;
nbsp;nbsp;nbsp;nbsp; lt;script src="/socket.io/socket.io.js"gt;lt;/scriptgt;
nbsp;nbsp;nbsp;nbsp; lt;scriptgt;
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; var socket=io.connect();
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; socket.on("news", function (data) {
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; console.log(data.hello);
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; socket.emit("my other event",{my:"data"});
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; });nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;
nbsp;nbsp;nbsp;nbsp; lt;/scriptgt;
nbsp;lt;/headgt;
nbsp;lt;bodygt;
nbsp;
nbsp;lt;/bodygt;

运行结果:、
nbsp;

node中socket.io的事件使用详解
可以发现一点:执行总是在监听端,而不是手动执行端.

案例2:在手动触发对方事件时,指定回调函数.

当客户端和服务端连接后,向客户端发送setName事件,事件携带"张三",触发事件时,指定一个回调函数,该回调函数向控制台输出2个参数值.

复制代码 代码如下:
nbsp;var http=require("http");
nbsp;var sio=require("socket.io");
nbsp;var fs=require("fs");
nbsp;var server=http.createServer(function (req,res) {
nbsp;nbsp;nbsp;nbsp; res.writeHead(200,{"Content-type":"text/html"});
nbsp;nbsp;nbsp;nbsp; res.end(fs.readFileSync("./index.html"));
nbsp;});
nbsp;server.listen(1337);
nbsp;var socket=sio.listen(server);
nbsp;socket.on("connection", function (socket) {
nbsp;nbsp;nbsp;nbsp; socket.emit("setName","张三", function (data1,data2) {
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; console.log(data1);
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; console.log(data2);
nbsp;nbsp;nbsp;nbsp; });
nbsp;});

复制代码 代码如下:
nbsp;lt;!DOCTYPE htmlgt;
nbsp;lt;htmlgt;
nbsp;lt;head lang="en"gt;
nbsp;nbsp;nbsp;nbsp; lt;meta charset="UTF-8"gt;
nbsp;nbsp;nbsp;nbsp; lt;titlegt;lt;/titlegt;
nbsp;nbsp;nbsp;nbsp; lt;script src="/socket.io/socket.io.js"gt;lt;/scriptgt;
nbsp;nbsp;nbsp;nbsp; lt;scriptgt;
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; var socket=io.connect();
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; socket.on("setName", function (name,fn) {
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; console.log(name);
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; fn("李四","王五");
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; });nbsp;nbsp;nbsp;nbsp;nbsp;
nbsp;nbsp;nbsp;nbsp; lt;/scriptgt;
nbsp;lt;/headgt;
nbsp;lt;bodygt;
nbsp;
nbsp;lt;/bodygt;
nbsp;lt;/htmlgt;

执行结果:

node中socket.io的事件使用详解

回调函数实在触发端执行的.

(编辑:常州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读