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

node.js中使用socket.io制作命名空间

发布时间:2016-11-24 16:05:45 所属栏目:Linux 来源:站长网
导读:如果开发者想在一个特定的应用程序中完全控制消息与事件的发送,只需要使用一个默认的"/"命名空间就足够了.但是如果开发者需要将应用程序作为第三方服务提供给其他应用程序,则需要为一个用于与客户端连接的socket端口定义一个独立的命名空间. io.of(namespa

如果开发者想在一个特定的应用程序中完全控制消息与事件的发送,只需要使用一个默认的"/"命名空间就足够了.但是如果开发者需要将应用程序作为第三方服务提供给其他应用程序,则需要为一个用于与客户端连接的socket端口定义一个独立的命名空间.

io.of(namespace)

制作两个命名空间

chat和news然后在客户端相互发送信息.

复制代码 代码如下:
var express=require("express");
var http=require("http");
var sio=require("socket.io");
var app=express();
var server=http.createServer(app);
app.get("/", function (req,res) {
nbsp;nbsp;nbsp; res.sendfile(__dirname+"/index.html");
});
server.listen(1337,"127.0.0.1", function () {
nbsp;nbsp;nbsp; console.log("开始监听1337");
});
var io=sio.listen(server);
var chart=io.of("/chat").on("connection", function (socket) {
nbsp;nbsp;nbsp; socket.send("欢迎访问chat空间!");
nbsp;nbsp;nbsp; socket.on("message", function (msg) {
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; console.log("chat命名空间接收到信息:"+msg);
nbsp;nbsp;nbsp; });
});
var news=io.of("/news").on("connection", function (socket) {
nbsp;nbsp;nbsp; socket.emit("send message","欢迎访问news空间!");
nbsp;nbsp;nbsp; socket.on("send message", function (data) {
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; console.log("news命名空间接受到send message事件,数据为:"+data);
nbsp;nbsp;nbsp; });
});

复制代码 代码如下:
lt;!DOCTYPE htmlgt;
lt;htmlgt;
lt;head lang="en"gt;
nbsp;nbsp;nbsp; lt;meta charset="UTF-8"gt;
nbsp;nbsp;nbsp; lt;titlegt;lt;/titlegt;
nbsp;nbsp;nbsp; lt;script src="/socket.io/socket.io.js"gt;lt;/scriptgt;
nbsp;nbsp;nbsp; lt;scriptgt;
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; var chat=io.connect("http://localhost/chat"),
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; news=io.connect("http://localhost/news");
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; chat.on("connect", function () {
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; chat.send("你好.");
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; chat.on("message", function (msg) {
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; console.log("从char空间接收到消息:"+msg);
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; });
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; });
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; news.on("connect", function () {
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; news.emit("send message","hello");
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; news.on("send message", function (data) {
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; console.log("从news命名空间接收到send message事件,数据位:"+data);
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; });
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; });
nbsp;nbsp;nbsp; lt;/scriptgt;
lt;/headgt;
lt;bodygt;
lt;/bodygt;
lt;/htmlgt;

运行结果:

node.js中使用socket.io制作命名空间

小伙伴们是否了解了在node.js中使用socket.io制作命名空间的方法了呢,这里的2个例子很简单,童鞋们自由发挥下。

(编辑:常州站长网)

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

    热点阅读