# 1-5 发送事件(Emiting events)

> 使用`socket.io`推送二进制的文件，比如音频、视频等，emmm，突然想到我那个项目。<https://github.com/veaba/express-nuxt，里面涉及到一个live> 的page，可以翻译LOL 的视频直播字幕的转换，期待。。

## 发送事件

`socket.io`背后的主要思想是：你可以使用你想要的任何数据发送和接受你想要的任何事件，比如可以编码为json的对象，甚至也支持二进制数据。

让我们这样做，以便当用户键入消息时，服务器将其作为聊天消息事件获取。 `index.html`中的`scripts`部分现在应该如下所示：

```markup
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
  $(function () {
    var socket = io();
    $('form').submit(function(e){
      e.preventDefault(); // prevents page reloading 放置页面重新加载
      socket.emit('chat message', $('#m').val());
      $('#m').val('');
      return false;
    });
  });
</script>
```

然后， 在`index.js`中，我们打印出来`chat message` 事件：

```javascript
    io.on('connection',(socket)=>{
        socket.on('chat message',(msg)=>{
            console.log('message:'+msg)
        })
    })
```

> 提醒，关于以上代码的路径，是实际开发中，存在偏差，主要是jquery的路径，如果是本地文件，则需要做一层设置，以下为笔者的设置：

在`index.js`中，增加一个`static`目录，存放`jquery`的本地文件

```javascript
    app.use(express.static('test/static'))
```

在 `index.html`中

```markup
<script src="/socket.io/socket.io.js"></script><!--这个部分，在node端，会添加一层中间默认路由给它自己-->
<script src="jquery-1.11.1.js"></script>
```

结果将会像这个视频展示的一样：<https://i.cloudup.com/transcoded/zboNrGSsai.mp4>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://socket.gitbook.io/docs/1-zhi-nan-guide/emitting_events.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
