使用uniapp实现websocket聊天功能

简介: 使用uniapp实现websocket聊天功能

在APP.vue里面配置  URL里面填写的是自己的地址

<script>
  export default {
    onLaunch: function() {
      console.log('App Launch')
    },
    onShow: function() {
      console.log('App Show')
      // 获取用户id(发送者)
      let id = uni.getStorageSync('id')
      // 创建ws连接
      uni.connectSocket({
        url: ''
      });
      // 监听ws打开状态
      uni.onSocketOpen((res) => {
        // console.log(123);
        console.log('连接成功');
        // 绑定发送id
        // ws链接状态
        setInterval(() => {
          console.log(111)
          uni.sendSocketMessage({
            data: JSON.stringify({
              type: `ping`,
            })
          });
        }, 1500);
        console.log('WebSocket连接已打开!');
      });
      
      let ta = this
      // ws监听接受消息
      uni.onSocketMessage((res) => {
        console.log(res.data)
        console.log(JSON.parse(res.data), 'aaaa');
        if (JSON.parse(res.data).type == 'chat') {
          that.data.push({
            ...JSON.parse(res.data)
          })
        }
      });
      uni.onSocketError((res) => {
        console.log('WebSocket连接打开失败,请检查!', res);
        uni.connectSocket({
          url: ''
        });
      });
      uni.onSocketClose((res) => {
        console.log('WebSocket 已关闭!');
        uni.connectSocket({
          url: ''
        });
      });
    },
    onHide: function() {
      console.log('App Hide')
    }
  }
</script>
<style lang="scss">
  /*每个页面公共css */
  @import "@/uni_modules/uview-ui/index.scss";
</style>

聊天列表页面的实现  可以根据自己的样式做调整

<template>
  <view>
    <div style="height: 30rpx"></div>
    <!-- 消息列表 -->
    <div class="big" v-for="(item,index) in data" :key="index" @click="goTalk(item.receive,item.img)">
      <div style="width: 10px"></div>
      <image :src="'http://wcy.jingyi.icu/'+item.img" mode="" class="Ai"></image>
      <div style="width: 10px"></div>
      <div class="contentOne">
        <div style="height: 10px"></div>
        <div class="Tow">
          <p class="text">{{item.name}}</p>
          <p>{{item.addtime}}</p>
        </div>
        <div style="height: 20rpx"></div>
        <div class="titleCon">{{item.content}}</div>
        <div style="height: 10rpx"></div>
      </div>
    </div>
    <u-tabbar :value="value" :placeholder="false" @change="change1" :fixed="true" :safeAreaInsetBottom="false">
      <u-tabbar-item text="首页" icon="home" @click="change"></u-tabbar-item>
      <u-tabbar-item text="消息" icon="chat" :badge="num" @click="change"></u-tabbar-item>
    </u-tabbar>
  </view>
</template>

聊天记录页面布局的实现

<template>
  <view class="chat">
    <scroll-view :style="{height: `${windowHeight-inputHeight}rpx`}" id="scrollview" scroll-y="true"
      :scroll-top="scrollTop" class="scroll-view">
      <!-- 聊天主体 -->
      <view id="msglistview" class="chat-body">
        <!-- 聊天记录 -->
        <view v-for="(item,index) in data" :key="index">
          <!-- <view class="nows_time">
            <view class="am_pm_time">{{item.addtime}}</view>
          </view> -->
          <!-- 自己的内容 -->
          <view class="item self" v-if="item.sender == id">
            <!-- 文字内容 -->
            <view class="content right">
              {{item.content}}
            </view>
            <view class="avatar">
              <image :src="Himg" mode=""></image>
            </view>
          </view>
          <!-- 左侧内容 -->
          <view class="item Ai" v-if="item.sender != id">
            <view class="avatar">
              <image :src="Timg" mode=""></image>
            </view>
            <!-- 文字内容 -->
            <view class="content left">
              {{item.content}}
            </view>
          </view>
        </view>
      </view>
    </scroll-view>
    <!-- 防止聊天消息被发送框遮挡 -->
    <view class="chat-bottom" :style="{height: `${inputHeight}rpx`}">
      <view class="send-msg" :style="{bottom:`${keyboardHeight}rpx`}">
        <view class="uni-textarea">
          <textarea v-model="chatMsg" maxlength="300" confirm-type="send" @confirm="handleSend"
            :show-confirm-bar="false" @linechange="sendHeight" auto-height></textarea>
        </view>
        <button @tap="sends" class="send-btn">发送</button>
      </view>
    </view>
  </view>
</template>

 

相关文章
|
2月前
uniApp常用功能封装
uniApp常用功能封装
21 0
|
2月前
uniapp点击图片预览功能?
uniapp点击图片预览功能?
|
2月前
|
开发框架 网络协议 JavaScript
uniapp链接WebSocket 常用的api
uniapp链接WebSocket 常用的api
35 0
|
2月前
|
前端开发 搜索推荐 JavaScript
使用uniapp实现时钟功能
使用uniapp实现时钟功能
66 1
|
3月前
Uniapp 电子签名 包含 返回 撤回 颜色 线条等功能
Uniapp 电子签名 包含 返回 撤回 颜色 线条等功能
23 0
|
3月前
|
移动开发
Uniapp 多功能富文本编辑组件 可多端使用 H5插入
Uniapp 多功能富文本编辑组件 可多端使用 H5插入
34 0
|
4月前
|
开发工具
在 uniapp 上使用 mPaaS 的全屏扫码功能
在 uniapp 上使用 mPaaS 的全屏扫码功能
214 1
|
8天前
|
小程序
uniapp 实现当前页面分享至微信好友或朋友圈功能(带参数和无参数)
uniapp 实现当前页面分享至微信好友或朋友圈功能(带参数和无参数)
8 0
|
2月前
|
定位技术
uniapp实现地图电子围栏功能
uniapp实现地图电子围栏功能
69 0
|
2月前
uniapp前置摄像头拍照功能怎么实现?
uniapp前置摄像头拍照功能怎么实现?

热门文章

最新文章