本項(xiàng)目使用Gorilla WebSocket作為聊天室后臺(tái) , Gorilla WebSocket 基于Go語(yǔ)言開(kāi)發(fā),提供的demo實(shí)例中有聊天室源碼,不需要任何修改就能生成一個(gè)聊天室項(xiàng)目(包含后臺(tái)+web前臺(tái))一、聊天室 ...
本項(xiàng)目使用 Gorilla WebSocket 作為聊天室后臺(tái) , Gorilla WebSocket 基于Go語(yǔ)言開(kāi)發(fā),提供的demo實(shí)例中有聊天室源碼,不需要任何修改就能生成一個(gè)聊天室項(xiàng)目(包含后臺(tái)+web前臺(tái))
如果不想搭建服務(wù)器,可以跳過(guò)這個(gè)步驟,直接使用我的服務(wù)
1:安裝 golang , 安裝1.6以上版本,低版本問(wèn)題較多。注:下載golang需要翻墻
安裝Gorilla WebSocket模塊
go get github.com/gorilla/websocket啟動(dòng)聊天室
$ go get github.com/gorilla/websocket $ cd `go list -f '{{.Dir}}' github.com/gorilla/ websocket/examples/chat`$ go run *.go 3:web客戶端
訪問(wèn):http://ip:80801:將文件正確導(dǎo)入開(kāi)發(fā)者工具目錄。注意,websockets.js 文件地址。服務(wù)器域名,端口號(hào),如:url: http://121.42.51.70:8080 Git clone https://github.com/ericzyh/wechat-chat.git
2:使用微信web開(kāi)發(fā)者工具導(dǎo)入項(xiàng)目
3:修改服務(wù)地址 wechat-chat/utils/websockets.js 行1
如何通過(guò)js獲取到某個(gè)組件? (發(fā)送消息后,清空輸入框) A:
text存英文超過(guò)屏幕寬度后,會(huì)出現(xiàn)橫向滾動(dòng)條? A: 和css一樣加上樣式:word-break:break-all;
1:發(fā)送消息后,清空輸入框 2:名字可點(diǎn)擊,實(shí)現(xiàn)1v1通訊 3:加入聊天間
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
先看下效果

話不多說(shuō) 直接上代碼 1.頁(yè)面代碼:
<swiper indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}"> <block wx:for="{{imgUrls}}"> <swiper-item> <navigator url="{{item.link}}" hover-class="navigator-hover"> <image src="{{item.url}}" class="slide-image" width="355" height="150"/> </navigator> </swiper-item> </block> </swiper> <block> <input type="text" class="input-text" bindchange="setInputValue" placeholder="請(qǐng)輸入你要咨詢的內(nèi)容"/> <button bindtap="getgetinputSEnd" class="input-button">發(fā)送</button> </block> <view class="chat-area"> <view wx:for="{{msgs}}" wx:for-index="idx" wx:for-item="itemName"> <view class="say-title"> <block wx:if="{{idx%2 != 0}}"><text class="red-font">客服:</text></block> <block wx:if="{{idx%2 == 0}}"><text class="green-font">你:</text></block> </view> <view class="say-content"> <block wx:if="{{itemName.type == 'video'}}"> <video src="{{itemName.msg}}"></video> </block> <block wx:if="{{itemName.type == 'voice'}}"> <audio src="{{itemName.msg}}" controls loop></audio> </block> <block wx:if="{{itemName.type == 'image'}}"> <image src="{{itemName.msg}}"></image> </block> <block wx:if="{{itemName.type == 'text'}}"> <text>{{itemName.msg}}</text> </block> </view> </view> </view> 頁(yè)面中 使用了 模板的
條件渲染 :https://mp.weixin.qq.com/debug/wxadoc/dev/framework/view/wxml/list.html?t=1476197490824 列表渲染:https://mp.weixin.qq.com/debug/wxadoc/dev/framework/view/wxml/conditional.html?t=1476197492981
2. 樣式代碼
.slide-image{ width: 100%; } .input-text{ border:1px solid #abcdef; width:88%; background:#ddd; line-height:100%; text-indent: 0.5rem; margin:1rem auto; height:40px; } .input-button{ background:#48C23D; margin:0.5rem 5%; color:#fff; } .chat-area{ width: 90%; margin