# socket.io-rooms

> socket.io rooms

Latest version **1.0.1** (published 2019-10-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install socket.io-rooms
pnpm add socket.io-rooms
yarn add socket.io-rooms
bun add socket.io-rooms
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2019-10-15 |
| First published | 2019-08-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | tianxiangbing |
| Maintainers | tianxiangbing |
| Keywords | socket, websokcet |

## Links

- npm: https://www.npmjs.com/package/socket.io-rooms
- Repository: https://github.com/tianxiangbing/rooms
- Homepage: https://github.com/tianxiangbing/rooms#readme
- Issues: https://github.com/tianxiangbing/rooms/issues
- npm.io page: https://npm.io/package/socket.io-rooms

## Dependencies (1)

- [socket.io](https://npm.io/package/socket.io.md) ^2.2.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2019-10-15
- 1.0.0 — 2019-08-14

## README

# 多房间聊天室案例
本案例采用socket.io实现多房间多人聊天，主要实现了两个类，一个是用户`User`,一个是房间`Rooms`类
# 安装使用
```
npm install socket.io-rooms --save
```
# demo演示
把项目clone下来后，执行`npm start`,然后打开`example/index.html`即可品尝到演示效果
# 使用方式
## 服务端
```
const {User,Rooms}  = require('socket.io-rooms')
const server = require('http').createServer();
const io = require('socket.io')(server);

//大厅
io.on('connection', client => {
    let user = new User();
    client.emit('user',user);
    client.on('join', data => {
        /* 加入某个房间 */
        Rooms.join(data,user,io,client)
    });
    client.on('message',msg=>{
        if(user.roomId){
            // io.to(user.roomId).emit('message',msg)
            if(msg.type == 'update'){
                user.update(msg.body);
            }
            msg.user = user.uid;
            Rooms.send(user.roomId,msg)
        }else{
            io.emit('message',msg)
        }
        console.log(msg)
    })
    client.on('disconnect', () => {
        /* … */
        console.log("连接断开")
        Rooms.leave(user)
    });
});
server.listen(80);
```
这里传输统一使用`JSON`格式，消息`title`也以`message`为主,这里端口写的80，你可以使用其他端口，如果你是Express，也可以共用80端口。
## 客户端调用
```
const socket = io('http://localhost');
log =(...args)=>{
    document.getElementById('log').innerHTML +='<br/>'+args.map(item=>JSON.stringify(item)).join(' ')+'=>'+(+new Date());
} 

log(socket.id)
let user ={},room,client;
socket.on('connect', (c) => {
    log('connect ...', socket.id);
    socket.on('user',u=>{
        user = u;
        log('用户ID',u.uid)
    });
});
socket.on('message',msg=>{
    log('message:',msg)
});
function joinroom(num){
    //加入房间号为1的房间
    socket.emit('join',num);
}
function send(){
    let msg = document.getElementById('msg').value;
    socket.emit('message',{type:'TALK',body:msg})
    // setInterval(function(){
    //     socket.emit('message',{type:'TALK',body:+new Date()})
    // },2000)
}
```

---
_Source: https://npm.io/package/socket.io-rooms · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
