123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <script>
- var wsUrl="ws://42.194.245.189:8008/app/webSocket/w-";
- var pingpangTimes=null;
- var isSocketOpen=false;
- var socket=null;
- export default {
- data() {
- return {
- };
- },
- onLaunch: function() {
- console.log('App Launch')
- var that=this;
- uni.$on('initSocket', () => {
- that.initSocket()
- })
- uni.$on('sendMsg', (item) => {
- that.sendMsg(item)
- })
- uni.$on('closeWebSocket', () => {
- that.closeWebSocket()
- })
-
- },
- onShow: function() {
- console.log('App Show')
- },
- onHide: function() {
- console.log('App Hide')
- },
- onUnload() {
-
- console.log(pingpangTimes)
- clearInterval(pingpangTimes)
-
- },
- methods:{
-
- closeWebSocket(){
- if(socket!=null){
- uni.closeSocket();
- }
- clearInterval(pingpangTimes)
- },
- initSocket(){
- console.log("initSocket")
- //创建一个socket连接
- var userId=uni.getStorageSync('companyUserId') ;
- var that=this;
- console.log(wsUrl)
- socket=uni.connectSocket({
- url:wsUrl+userId,
- multiple:true,
- success: res=>{
- console.log('WebSocket连接已打开!');
- isSocketOpen=true
- //先确保清除了之前的心跳定时器
- clearInterval(pingpangTimes)
- uni.onSocketMessage((res)=>{
- console.log("收到消息")
- const redata = JSON.parse(res.data);
- console.log(redata);
- if(redata.cmd=="heartbeat"){
- //心跳
- console.log("heartbeat")
- }
- else if(redata.cmd=="getWeixinId"){
- uni.$emit('getWeixinId',redata);
- }
- else if(redata.cmd=="getWeixinList"){
- uni.$emit('getWeixinList',redata);
- }
- else if(redata.cmd=="sendSop"){
- uni.$emit('sendSop',redata);
- }
-
- })
- pingpangTimes=setInterval(()=>{
- var userId=uni.getStorageSync('userId') ;
- var data={cmd:"heartbeat",userId: "w-"+userId};
- console.log(data)
- uni.sendSocketMessage({
- data:JSON.stringify(data),
- success:()=>{
- console.log('WebSocket发送心条数据!');
- },
- fail:()=>{
- isSocketOpen=false
- }
- });
- },5000)
- },
- error: res=>{
- console.log(res)
- },
- })
- //监听socket打开
- uni.onSocketOpen(()=>{
- isSocketOpen=true
- console.log('WebSocket连接已打开!!');
- })
- //监听socket关闭
- uni.onSocketClose(()=>{
- isSocketOpen=false
- console.log('WebSocket连接已关闭!');
- })
- //监听socket错误
- uni.onSocketError(()=>{
- isSocketOpen=false
- console.log('WebSocket连接打开失败');
- })
-
- },
- sendMsg(data){
- if(isSocketOpen){
- var userId=uni.getStorageSync('companyUserId') ;
- uni.sendSocketMessage({
- data: JSON.stringify(data),
- success:()=>{
-
- console.log("发送成功")
- },
- fail:()=>{
- console.log("发送失败")
- }
- });
-
- }
-
- },
-
- }
- }
- </script>
- <style lang="scss">
- /*每个页面公共css */
- @import "@/uni_modules/uview-ui/index.scss";
- view{
- box-sizing: border-box;
- }
- .ellipsis{
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .ellipsis2{
- overflow:hidden;
- text-overflow:ellipsis;
- display:-webkit-box;
- -webkit-box-orient:vertical;
- -webkit-line-clamp:2;
- }
-
-
- .no-data-box{
- height:100%;
- width: 100%;
- display: flex;
- justify-content: center;
- align-items: center;
- flex-direction: column;
- image{
- width: 264upx;
- height: 212upx;
- }
- .empty-title{
- margin-top: 20rpx;
- font-size: 28rpx;
- color: gray;
-
- }
- }
- </style>
- <style lang="less">
- /*每个页面公共css */
- @import './assets/iconfont/iconfont.css';
- @import './assets/css/common.less';
- </style>
|