经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JSJS库框架 » JavaScript » 查看文章
详解如何使用koa实现socket.io官网的例子
来源:jb51  时间:2018/11/5 10:46:40  对本文有异议

socket.io官网中使用express实现了一个最简单的IM即时聊天,今天我们使用koa来实现一下

### 框架准备

1.确保你本地已经安装好了nodejs和npm,使用koa要求node版本>7.6

2.在你需要的位置新建一个文件夹(官网的简单命名为chat-example)

3.进入项目目录,创建package.json文件:

  1. {
  2. "name": "socket-chat-example",
  3. "version": "0.0.1",
  4. "description": "my first socket.io app",
  5. "dependencies": {}
  6. }

4.命令行中使用npm安装,执行以下命令

  1. npm install --save koa koa-router http fs socket.io

### 接下来直接上代码

项目目录下直接新建index.js

  1. var Koa = require('koa');
  2. var app = new Koa();
  3. const Router = require('koa-router');
  4. const fs = require('fs');
  5. const server = require('http').createServer(app.callback());
  6. const io = require('socket.io')(server);
  7.  
  8. // 首页路由
  9. let router = new Router();
  10. router.get('/', ctx => {
  11. ctx.response.type = 'html';
  12. ctx.response.body = fs.createReadStream('./index.html');
  13. });
  14. app.use(router.routes());
  15.  
  16. // socket连接
  17. io.on('connection', (socket) => {
  18. socket.on('chat message', (msg) => {
  19. console.log('message: '+msg);
  20. io.emit('chat message', msg);
  21. });
  22. socket.on('disconnect', () => {
  23. console.log('user disconnected');
  24. });
  25. });
  26.  
  27. // 监听端口
  28. server.listen(3000, () => {
  29. console.log('listening on *:3000');
  30. });
  31.  

重点:

socket的连接方式是先建立server,它的获取方式不再是:

  1. var http = require('http').Server(app);
  2. var io = require('socket.io')(http);

而变成了:

  1. const server = require('http').createServer(app.callback());
  2. const io = require('socket.io')(server);

node8之后,function(){} 可以简化为 () => {},写法上更加的简洁

页面index.html

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Socket.IO chat</title>
  5. <style>
  6. * { margin: 0; padding: 0; box-sizing: border-box; }
  7. body { font: 13px Helvetica, Arial; }
  8. form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
  9. form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
  10. form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
  11. #messages { list-style-type: none; margin: 0; padding: 0; }
  12. #messages li { padding: 5px 10px; }
  13. #messages li:nth-child(odd) { background: #eee; }
  14. </style>
  15. </head>
  16. <body>
  17. <ul id="messages"></ul>
  18. <form action="">
  19. <input id="m" autocomplete="off" /><button>Send</button>
  20. </form>
  21. <script src="/socket.io/socket.io.js"></script>
  22. <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
  23. <script>
  24. $(function () {
  25. var socket = io();
  26. $('form').submit(function(){
  27. socket.emit('chat message', $('#m').val());
  28. $('#m').val('');
  29. return false;
  30. });
  31. socket.on('chat message', function(msg){
  32. $('#message').append($('<li>').text(msg));
  33. });
  34. });
  35. </script>
  36. </body>
  37. </html>

index.html和官网的一样,不做任何改动

最后执行node index.js,打开浏览器,输入http://localhost:3000就可以实现最简单的聊天了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号