当前位置:   article > 正文

微信小程序WebSocket实现聊天对话功能完整源码_小程序 websocket源码

小程序 websocket源码

相关文章:

1.小程序聊天群,发送语音,文字,图片。

2.微信小程序集成腾讯IM,实现实时音视频通话,1V1聊天

3.云开发微信小程序聊天群

4.接入网易云信IM即时通讯的微信小程序聊天室

5.微信小程序聊天功能 WebSocket 实现发送文字,图片,语音以及WebSocket 常见问题解决方案

6.[微信小程序]聊天对话(文本,图片)的功能(完整代码附效果图)

如果有个性化的需要修改,可以联系我的微信。

微信小程序开发交流qq群   173683895

   承接微信小程序开发。扫码加微信。

正文:

js

  1. var app = getApp();
  2. var socketOpen = false;
  3. var frameBuffer_Data, session, SocketTask;
  4. var url = 'ws://请填写您的长链接接口地址';
  5. var upload_url ='请填写您的图片上传接口地址'
  6. Page({
  7. data: {
  8. user_input_text: '',//用户输入文字
  9. inputValue: '',
  10. returnValue: '',
  11. addImg: false,
  12. //格式示例数据,可为空
  13. allContentList: [],
  14. num: 0
  15. },
  16. // 页面加载
  17. onLoad: function () {
  18. this.bottom();
  19. },
  20. onShow: function (e) {
  21. if (!socketOpen) {
  22. this.webSocket()
  23. }
  24. },
  25. // 页面加载完成
  26. onReady: function () {
  27. var that = this;
  28. SocketTask.onOpen(res => {
  29. socketOpen = true;
  30. console.log('监听 WebSocket 连接打开事件。', res)
  31. })
  32. SocketTask.onClose(onClose => {
  33. console.log('监听 WebSocket 连接关闭事件。', onClose)
  34. socketOpen = false;
  35. this.webSocket()
  36. })
  37. SocketTask.onError(onError => {
  38. console.log('监听 WebSocket 错误。错误信息', onError)
  39. socketOpen = false
  40. })
  41. SocketTask.onMessage(onMessage => {
  42. console.log('监听WebSocket接受到服务器的消息事件。服务器返回的消息', JSON.parse(onMessage.data))
  43. var onMessage_data = JSON.parse(onMessage.data)
  44. if (onMessage_data.cmd == 1) {
  45. that.setData({
  46. link_list: text
  47. })
  48. console.log(text, text instanceof Array)
  49. // 是否为数组
  50. if (text instanceof Array) {
  51. for (var i = 0; i < text.length; i++) {
  52. text[i]
  53. }
  54. } else {
  55. }
  56. that.data.allContentList.push({ is_ai: true, text: onMessage_data.body });
  57. that.setData({
  58. allContentList: that.data.allContentList
  59. })
  60. that.bottom()
  61. }
  62. })
  63. },
  64. webSocket: function () {
  65. // 创建Socket
  66. SocketTask = wx.connectSocket({
  67. url: url,
  68. data: 'data',
  69. header: {
  70. 'content-type': 'application/json'
  71. },
  72. method: 'post',
  73. success: function (res) {
  74. console.log('WebSocket连接创建', res)
  75. },
  76. fail: function (err) {
  77. wx.showToast({
  78. title: '网络异常!',
  79. })
  80. console.log(err)
  81. },
  82. })
  83. },
  84. // 提交文字
  85. submitTo: function (e) {
  86. let that = this;
  87. var data = {
  88. body: that.data.inputValue,
  89. }
  90. if (socketOpen) {
  91. // 如果打开了socket就发送数据给服务器
  92. sendSocketMessage(data)
  93. this.data.allContentList.push({ is_my: { text: this.data.inputValue }});
  94. this.setData({
  95. allContentList: this.data.allContentList,
  96. inputValue: ''
  97. })
  98. that.bottom()
  99. }
  100. },
  101. bindKeyInput: function (e) {
  102. this.setData({
  103. inputValue: e.detail.value
  104. })
  105. },
  106. onHide: function () {
  107. SocketTask.close(function (close) {
  108. console.log('关闭 WebSocket 连接。', close)
  109. })
  110. },
  111. upimg: function () {
  112. var that = this;
  113. wx.chooseImage({
  114. sizeType: ['original', 'compressed'],
  115. success: function (res) {
  116. that.setData({
  117. img: res.tempFilePaths
  118. })
  119. wx.uploadFile({
  120. url: upload_url,
  121. filePath: res.tempFilePaths,
  122. name: 'img',
  123. success: function (res) {
  124. console.log(res)
  125. wx.showToast({
  126. title: '图片发送成功!',
  127. duration: 3000
  128. });
  129. }
  130. })
  131. that.data.allContentList.push({ is_my: { img: res.tempFilePaths } });
  132. that.setData({
  133. allContentList: that.data.allContentList,
  134. })
  135. that.bottom();
  136. }
  137. })
  138. },
  139. addImg: function () {
  140. this.setData({
  141. addImg: !this.data.addImg
  142. })
  143. },
  144. // 获取hei的id节点然后屏幕焦点调转到这个节点
  145. bottom: function () {
  146. var that = this;
  147. this.setData({
  148. scrollTop: 1000000
  149. })
  150. },
  151. })
  152. //通过 WebSocket 连接发送数据,需要先 wx.connectSocket,并在 wx.onSocketOpen 回调之后才能发送。
  153. function sendSocketMessage(msg) {
  154. var that = this;
  155. console.log('通过 WebSocket 连接发送数据', JSON.stringify(msg))
  156. SocketTask.send({
  157. data: JSON.stringify(msg)
  158. }, function (res) {
  159. console.log('已发送', res)
  160. })
  161. }

 

wxml

 

  1. <view class='page_bg' wx:if='{{block}}' bindtap='hide_bg' />
  2. <view class='btn_bg' wx:if='{{block}}'>
  3. <view wx:for="{{link_list}}" wx:key='index'>
  4. <button class="sp_tit" id='{{index}}' bindtap='list_item'>查看详情 {{item}} </button>
  5. </view>
  6. </view>
  7. <scroll-view class="history" scroll-y="true" scroll-with-animation scroll-top="{{scrollTop}}">
  8. <block wx:key="{{index}}" wx:for="{{allContentList}}">
  9. <!-- <view>
  10. <text class='time'>{{time}}</text>
  11. </view> -->
  12. <view class='my_right' wx:if="{{item.is_my}}">
  13. <view class='p_r' wx:if='{{item.is_my.text}}'>
  14. <text class='new_txt'><text class='new_txt_my'>{{item.is_my.text}}</text></text>
  15. <view class='sanjiao my'></view>
  16. <image class='new_img' src='/images/test.jpg'></image>
  17. </view>
  18. <view class='p_r' wx:if='{{item.is_my.img}}' bindtap='my_audio_click' data-id='{{index}}'>
  19. <text class='new_txt'> </text>
  20. <view class='my_img_bg'>
  21. <image class='my_audio' src='{{img}}'></image></view>
  22. <view class='sanjiao my'></view>
  23. <image class='new_img' src='/images/test.jpg'></image>
  24. </view>
  25. </view>
  26. <!-- <view class='you_left' id='id_{{allContentList.length}}'> -->
  27. <view class='you_left' id='id_{{allContentList.length}}' wx:key="{{index}}" wx:if="{{item.is_ai}}">
  28. <view class='p_r'>
  29. <image class='new_img' src='/images/chac.jpg'></image>
  30. <view class='sanjiao you'></view>
  31. <view class='new_txt'>
  32. <view class='new_txt_ai'>
  33. <!-- {{item.text}} -->
  34. <block wx:for='{{item.is_two}}' wx:key='index'>
  35. <text wx:if='{{item.text}}'>{{item.text}}</text>
  36. <text wx:if='{{item.a.title}}' style='color:#0000EE' bindtap='link' id='{{item.a.link}}'>{{item.a.title}}</text>
  37. </block>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. </block>
  43. </scroll-view>
  44. <view class="sendmessage">
  45. <image class='voice_icon' bindtap='addImg' src='/images/jia_img.png'></image>
  46. <block wx:if='{{!addImg}}'>
  47. <input type="text" bindinput="bindKeyInput" value='{{inputValue}}' focus='{{focus}}' bindfocus="focus" confirm-type="done" placeholder="" />
  48. <button bindtap="submitTo" class='user_input_text'>发送</button>
  49. </block>
  50. <block wx:if='{{addImg}}'>
  51. <view class='voice_ing' bindtap="upimg">发送图片</view>
  52. </block>
  53. </view>

css

  1. page {
  2. background-color: #f2f2f2;
  3. height: 100%;
  4. }
  5. .jia_img{
  6. height: 80rpx;
  7. width: 90rpx;
  8. }
  9. .time {
  10. text-align: center;
  11. padding: 5rpx 20rpx 5rpx 20rpx;
  12. width: 200rpx;
  13. font-size: 26rpx;
  14. background-color: #E8E8E8;
  15. }
  16. .tab{
  17. bottom: 120rpx;
  18. }
  19. .tab_1{
  20. position: fixed;
  21. bottom: 50rpx;
  22. width: 200rpx;
  23. font-size: 26rpx;
  24. left: 50%;
  25. margin-left: -45rpx;
  26. height: 100rpx;
  27. }
  28. .tab_2{
  29. right: 30rpx;
  30. position: fixed;
  31. }
  32. /* 聊天 */
  33. .my_right {
  34. float: right;
  35. margin-top: 30rpx;
  36. position: relative;
  37. right: 40rpx;
  38. }
  39. .my_audio{
  40. height: 120rpx;
  41. width: 150rpx;
  42. position: absolute;
  43. right: 150rpx;
  44. background: white;
  45. top: 20rpx;
  46. }
  47. .my_img_bg{
  48. height: 150rpx;
  49. width: 400rpx;
  50. position: relative;
  51. right: 0;
  52. background: white;
  53. top: 20rpx;
  54. }
  55. .you_left {
  56. margin-top: 30rpx;
  57. float: left;
  58. position: relative;
  59. left: 5rpx;
  60. }
  61. .new_img {
  62. width: 100rpx;
  63. height: 100rpx;
  64. border-radius: 50%;
  65. }
  66. .new_txt {
  67. width: 420rpx;
  68. }
  69. .new_txt_my{
  70. border-radius: 7px;
  71. background-color: #fff;
  72. margin-top: 10rpx;
  73. padding: 0rpx 30rpx 0rpx 30rpx;
  74. float: right;
  75. }
  76. .new_txt_ai{
  77. border-radius: 7px;
  78. background-color: #fff;
  79. margin-top: 10rpx;
  80. padding: 0rpx 30rpx 0rpx 30rpx;
  81. float: left;
  82. }
  83. .sanjiao {
  84. top: 25rpx;
  85. position: relative;
  86. width: 0px;
  87. height: 0px;
  88. border-width: 15rpx;
  89. border-style: solid;
  90. }
  91. .my {
  92. border-color: transparent transparent transparent #fff;
  93. }
  94. .you {
  95. border-color: transparent #fff transparent transparent;
  96. }
  97. .sendmessage {
  98. width: 100%;
  99. z-index: 2;
  100. display: flex;
  101. position: fixed;
  102. bottom: 0px;
  103. background-color: #F4F4F6;
  104. flex-direction: row;
  105. height: 85rpx;
  106. }
  107. .voice_icon{
  108. width: 60rpx;
  109. height: 60rpx;
  110. margin: 0 auto;
  111. padding: 10rpx 10rpx 10rpx 10rpx;
  112. }
  113. .voice_ing{
  114. width: 90%;
  115. height: 75rpx;
  116. line-height: 85rpx;
  117. text-align: center;
  118. border-radius: 15rpx;
  119. border: 1px solid #d0d0d0;
  120. }
  121. .sendmessage_2 {
  122. z-index: 1;
  123. position: relative;
  124. width: 100%;
  125. display: flex;
  126. background-color: #F4F4F6;
  127. flex-direction: row;
  128. height: 85rpx;
  129. }
  130. .sendmessage input {
  131. width: 75%;
  132. height: 60rpx;
  133. background-color: white;
  134. line-height: 60rpx;
  135. font-size: 28rpx;
  136. border-radius: 10rpx;
  137. margin-top: 10rpx;
  138. margin-left: 20rpx;
  139. border: 1px solid #d0d0d0;
  140. padding-left: 20rpx;
  141. }
  142. .sendmessage button {
  143. border: 1px solid white;
  144. width: 18%;
  145. height: 65rpx;
  146. background: #00CC00;
  147. color: white;
  148. line-height: 65rpx;
  149. margin-top: 10rpx;
  150. font-size: 28rpx;
  151. }
  152. .hei{
  153. height: 20rpx;
  154. }
  155. .history {
  156. height: 80%;
  157. padding: 20rpx 20rpx 20rpx 20rpx;
  158. font-size: 14px;
  159. line-height: 50rpx;
  160. word-break: break-all;
  161. }
  162. .icno_kf{
  163. position: fixed;
  164. bottom: 160rpx;
  165. margin: 0 auto;
  166. text-align: center;
  167. left: 50%;
  168. margin-left: -40rpx;
  169. width: 100rpx;
  170. height: 100rpx;
  171. border-radius: 50%
  172. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/232561
推荐阅读
相关标签
  

闽ICP备14008679号