express+socket.io实现一个聊天室效果代码
代码语言:nodejs
所属分类:通讯
代码描述:express+socket.io实现一个聊天室效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
// 使用 express 框架 var app = require('express')(); var express = require("express"); var server = require('http').Server(app); // 引入 socket.io var io = require('socket.io')(server,{ allowEIO3: true // false by default }); //app.use(express.static("/")); // 监听 80 端口 server.listen(3001); // 开启静态资源服务 // io 各种事件 io.on('connection', function (socket) { console.log('websocket has connected') socket.emit('message', { hello: '欢迎链接' }); socket.on('my other event', function (data) { console.log(data); socket.emit('message', { hello: '发送成功' }); }); socket.on('say', function (data) { console.log(data); if (data.my === '走,一起吃饭吧') { io.sockets.emit('eating', { hello: '果断走起呀!' }); return } io.sockets.emit('news', { hello: data.my }); }); }); app.get('/test', function(req, res) { // res.send(new Buffer('whoop')); // res.send({ // some: 'json' //}); var str = ` <!DOCTYPE html> <html lang="en"> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/socket.io.js"></script> <head> <meta charset="UTF-8"> <title>Document</title> <style> input { background-color: #fff; background-image: none; border-radius: 4px; border: 1px solid #bfcbd9; box-sizing: border-box; color: #1f2d3d; font-size: inherit; height: 40px; line-height: 1; outline: 0; padding: 3px 10px; } .el-button--primary { color: #fff; background-color: #20a0ff; border-color: #20a0ff; } .el-button { display: inline-block; line-height: 1; white-space: nowrap; cursor: pointer; background: #00aac5; border: 1px solid #c4c4c4; color: #fff; margin: .........完整代码请登录后点击上方下载按钮下载查看
网友评论0