Vue+ssh框架實(shí)現(xiàn)在線聊天
本文實(shí)例為大家分享了Vue+ssh框架實(shí)現(xiàn)在線聊天的具體代碼,供大家參考,具體內(nèi)容如下
效果圖


核心部分
websocket編程
向后臺(tái)發(fā)送消息
<template>
<el-container>
<el-header >
</el-header>
<el-main>
<div class="cht">
<div v-for="(d,index) in mycontent" :key="index">
<my :message="d.mess" :time="d.time" :bl="d.bl"></my>
</div>
</div>
<div class="smess">
<el-row>
<el-col :span="18">
<el-input type="textarea" placeholder="請(qǐng)輸入內(nèi)容" v-model="textarea" class="text"></el-input>
</el-col>
<el-col :span="6">
<br>
<el-button type="primary" round @click="mess()">發(fā)送消息</el-button>
</el-col>
</el-row>
</div>
</el-main>
</el-container>
</template>
<style>
.smess{
left: 20%;
width:70%;
position: absolute;
top:70%
}
.text{
border: 1px solid #409eff;
}
.cht{
width: 55%;
height: 30%;
background-color: burlywood;
margin-left: 18%;
}
</style>
<script>
import router from "../../router/router.js";
import my from "./my";
import axios from "axios";
import Qs from "qs";
var mylogo=localStorage.getItem("logo");//當(dāng)前的的用戶頭像
var identity=localStorage.getItem("identity");//當(dāng)前身份
var name=localStorage.getItem("username");//當(dāng)前用戶名
//從上一個(gè)頁面獲取一個(gè)老師名稱
var teacher='';
export default {
components: {
my
},
methods: {
//在方法里調(diào)用this.websocketsend()發(fā)送數(shù)據(jù)給服務(wù)器
onConfirm() {
//需要傳輸?shù)臄?shù)據(jù)
var data="你好";
this.websocketsend(JSON.stringify(data));
},
//點(diǎn)擊發(fā)送把消息給后臺(tái)
mess(){
var mydata=this.textarea;
let data = {msg: mydata};
this.websocketsend(JSON.stringify(data));
},
/* */
initWebSocket() {
// 初始化weosocket
//獲取當(dāng)前的用戶名
this.websock = new WebSocket(
"ws://localhost:8080/PsychoSys/javasocket/" +name
);
this.websock.onmessage = this.websocketonmessage;
this.websock.onerror = this.websocketonerror;
this.websock.onopen = this.websocketonopen;
this.websock.onclose = this.websocketclose;
},
websocketonopen() {
// 連接建立之后執(zhí)行send方法發(fā)送數(shù)據(jù)
let data = { code: 0, msg: "這是client:初次連接" };
},
websocketonerror() {
console.log("WebSocket連接失敗");
},
websocketonmessage(e) {
// 數(shù)據(jù)接收
var s=eval('(' + e.data + ')');
//把數(shù)據(jù)都插入到里面去
this.mycontent.push({mess:s.msg,time:s.date,bl:s.isSelf,mylogo:mylogo});
},
websocketsend(Data) {
// 數(shù)據(jù)發(fā)送
this.websock.send(Data)
},
websocketclose(e) {
// 關(guān)閉
console.log("已關(guān)閉連接", e);
}
},
created() {
console.log("created");
this.initWebSocket();
},
data() {
return {
websocket: null,
textarea:'' ,
mycontent:[],
iden:true
};
},
destroyed() {
this.websock.close();
}
};
</script>
組件my.vue
<template>
<div v-if="bl" class="rborders">
<el-row class="ms">
<el-col :span="22">
<el-row><span>{{message}}</span></el-row>
<br>
<el-row><span class="time">{{time}}</span></el-row>
</el-col>
<el-col :span="2" >
<img src="mylogo" class="logo"/>
</el-col>
</el-row>
</div>
<div v-else class="lborders">
<el-row>
<el-col :span="2" >
<img src="http://localhost:8080/PsychoSys/title/user.png" class="logo"/>
</el-col>
<br>
<el-col :span="12">
<el-row >
<el-col :span="24"><span >{{message}}</span></el-col>
</el-row>
<br>
<el-row><span class="time">{{time}}</span></el-row>
</el-col>
</el-row>
</div>
</template>
<style>
.ms{
text-align: right;
margin-right: 0%;
}
.logo{
width:60px;
height: 60px;
border-radius: 50%;
}
.time{
font-size:14px;
}
.lborders{
position: relative;
margin-left:0%;
}
.rborders{
position: relative;
margin-right:0%;
}
</style>
<script>
export default {
props: ['message','time','bl','mylogo'],
data() {
return {
};
},
}
</script>
后臺(tái)代碼
package cn.com.socket;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.hibernate.SessionFactory;
import net.sf.json.JSONObject;
@ServerEndpoint("/javasocket/{uname}")
public class SocketPart {
//日期化
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//存儲(chǔ)會(huì)話的集合,value類型是java類class SocketPart
private static Map<String,SocketPart> map=new ConcurrentHashMap<String,SocketPart>();
private String username;
private Session session;
private SessionFactory sf;
public SessionFactory getSf() {
return sf;
}
public void setSf(SessionFactory sf) {
this.sf = sf;
}
@OnOpen
public void open(@PathParam("uname")String username,Session session){
this.username=username;
this.session=session;
map.put(username,this);
}
@OnClose
public void close(){
map.remove(this.username);
try {
this.session.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("關(guān)閉");
}
@OnError
public void error(Throwable t) {
// 添加處理錯(cuò)誤的操作
close();
System.out.println("發(fā)生錯(cuò)誤");
t.printStackTrace();
}
@OnMessage
public void mess(String message,Session session){
JSONObject jsonObject = JSONObject.fromObject(message);
jsonObject.put("date", DATE_FORMAT.format(new Date()));
//把當(dāng)前集合的大小給前臺(tái),不然的話,就不知道怎么存儲(chǔ)
jsonObject.put("cusize",map.size());
//接收到信息
for (String s : map.keySet()) {
if(this.username.equals(map.get(s).username)){
jsonObject.put("isSelf", true);
}else{
jsonObject.put("isSelf", false);
}
map.get(s).session.getAsyncRemote().sendText(jsonObject.toString());
}
}
}
注意:導(dǎo)入兩個(gè)包

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue+express+Socket實(shí)現(xiàn)聊天功能
- Vue實(shí)現(xiàn)聊天界面
- vue實(shí)現(xiàn)web在線聊天功能
- vue+web端仿微信網(wǎng)頁版聊天室功能
- Vue.js仿微信聊天窗口展示組件功能
- vue + socket.io實(shí)現(xiàn)一個(gè)簡易聊天室示例代碼
- 基于Vue2實(shí)現(xiàn)的仿手機(jī)QQ單頁面應(yīng)用功能(接入聊天機(jī)器人 )
- Vue Cli 3項(xiàng)目使用融云IM實(shí)現(xiàn)聊天功能的方法
- vue實(shí)現(xiàn)的微信機(jī)器人聊天功能案例【附源碼下載】
- 基于vue和websocket的多人在線聊天室
相關(guān)文章
Vue基于el-breadcrumb實(shí)現(xiàn)面包屑功能(操作代碼)
這篇文章主要介紹了Vue基于el-breadcrumb實(shí)現(xiàn)面包屑功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
vue初嘗試--項(xiàng)目結(jié)構(gòu)(推薦)
這篇文章主要介紹了vue初嘗試--項(xiàng)目結(jié)構(gòu)的相關(guān)知識(shí),需要的朋友可以參考下2018-01-01
vue中數(shù)據(jù)綁定值(字符串拼接)的幾種實(shí)現(xiàn)方法
這篇文章主要介紹了vue中數(shù)據(jù)綁定值(字符串拼接)的幾種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vue-cli 為項(xiàng)目設(shè)置別名的方法
這篇文章主要介紹了vue-cli 為項(xiàng)目設(shè)置別名的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
vue實(shí)現(xiàn)上傳圖片添加水印(升級(jí)版)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)上傳圖片添加水印的升級(jí)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09

