本文摘自PHP中文网,作者青灯夜游,侵删。

相关推荐:《node js教程》
最近学习了一下websocket的即时通信,感觉非常的强大,这里我用node启动了一个服务进行websocket链接,然后再vue的view里面进行了链接,进行通信,废话不多说,直接上代码吧,
首先,我需要用到node的nodejs-websocket模块
使用yarn进行安装
1 | yarn add nodejs-websocket --save
|
当然,你也可以用npm进行安装
1 | npm i nodejs-websocket --save
|
安装完毕之后,我们开始写服务端的代码,首先,我用node在本地起了一个node服务器用来开启websocket服务
sock.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | let ws = require( "nodejs-websocket" );
console.log( "开始建立链接" );
ws.createServer(function (conn) {
conn.on( "text" , function (str) {
console.log( "收到的信息为" , str);
conn.send(`${str}(机器人`)
});
conn.on( "close" , function (code, reason) {
console.log( "关闭连接" )
});
conn.on( "error" , function (code, reason) {
console.log( "异常关闭" )
})
}).listen( 8001 );
console.log( "链接建立完毕" );
|
服务端主要是用nodejs-websocket用来开启服务,以及返回前端需要的值,这里我只是做了一个简单的处理,在接受值得后面加了一个‘机器人’的string,
然后,我们需要开启这个node服务,

命令后面的路径一定要找对,我是把sock.js放在了根目录的socket文件夹下面
执行
最后,看我们的客户端,客户端我是想有一个输入框,然后有个聊天框:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | <template>
<p class = "test3" >
<p class = "msg" ref= "box" >
<p v- for = "item in list" : class = "[item.type,'msg-item']" >
<p>
{{item.content}}
</p>
</p>
</p>
<p class = "input-group" >
<input type= "text" v-model= "contentText" >
<button @click = "sendText" >发送</button>
</p>
</p>
</template>
<script>
export default {
name: "index3" ,
data() {
return {
list: [],
contentText: "" ,
}
},
methods: {
sendText() {
let that = this ;
this .list = [... this .list, {type: "mine" , content: this .contentText}];
this .backText(function () {
that.contentText = "" ;
});
},
backText(callback) {
let that = this ;
if (window.WebSocket) {
let ws = new WebSocket( "ws://192.168.11.169:8001" );
ws.onopen = function (e) {
console.log( "链接服务器成功" );
console.log( "that.contentText is" , that.contentText);
ws.send(that.contentText);
callback();
};
ws.onclose = function (e) {
console.log( "服务器关闭" )
};
ws.onerror = function () {
console.log( "服务器出错" )
};
ws.onmessage = function (e) {
that.list = [...that.list, {type: "robot" , content: e.data}]
}
}
}
},
watch: {
list: function () {
let that = this ;
setTimeout(() => {
that.$refs.box.scrollTop = that.$refs.box.scrollHeight;
}, 0 );
}
},
mounted() {
}
};
</script>
<style scoped lang= "scss" >
.test3 {
text-align: center;
}
.msg {
width: 100px;
height: 100px;
overflow: auto;
padding-top: 5px;
border: 1px solid red;
display: inline-block;
margin-bottom: 6px;
.msg-item {
position: relative;
overflow: hidden;
p {
display: inline-block;
border-radius: 40px;
background: #3C3D5A;
color: white;
float : left;
padding: 2px 12px;
margin: 0 0 2px 0 ;
max-width: 70 %;
text-align: left;
box-sizing: border-box;
}
&.mine {
p {
float : right;
background: aquamarine;
color: white;
}
}
}
}
</style>
|
看一下最终效果:


相关推荐:
2020年前端vue面试题大汇总(附答案)
vue教程推荐:2020最新的5个vue.js视频教程精选
更多编程相关知识,请访问:编程教学!!
以上就是node+vue怎么实现简单的WebSocket聊天功能?(代码示例)的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
vue.js就是Node吗
浅谈vue.js中$refs的使用方法(附代码)
vue界面刷新不显示怎么办
vue通常在项目中干什么
用vue.js用什么编辑器
vue子组件怎么向父组件传值
spring websocket的详细介绍
什么是 vue
浅谈vue项目中使用npm安装bootstrap和jquery
怎么测试Node有没有安装好
更多相关阅读请进入《Node》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » node+vue怎么实现简单的WebSocket聊天功能?(代码示例)