mirror of
https://github.com/real-zony/ZonyLrcToolsX.git
synced 2025-09-03 03:26:53 +00:00
feat: Establish Websocket communication.
This commit is contained in:
@@ -50,6 +50,9 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import Socket from "@/communication/socket";
|
||||||
|
import {mapMutations} from "vuex"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
data: () => ({
|
data: () => ({
|
||||||
drawer: null,
|
drawer: null,
|
||||||
@@ -59,5 +62,19 @@ export default {
|
|||||||
['mdi-information', '关于', '/about'],
|
['mdi-information', '关于', '/about'],
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
|
created() {
|
||||||
|
Socket.$on("message", this.handleGetMessage);
|
||||||
|
},
|
||||||
|
beforeDestroy() {
|
||||||
|
Socket.$off("message", this.handleGetMessage);
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
...mapMutations({
|
||||||
|
setWsRes: "ws/setWsRes",
|
||||||
|
}),
|
||||||
|
handleGetMessage(msg) {
|
||||||
|
this.setWsRes(JSON.parse(msg));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
29
src/ui/src/communication/socket.js
Normal file
29
src/ui/src/communication/socket.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import Vue from 'vue'
|
||||||
|
|
||||||
|
// const wsUrl = process.env.VUE_APP_WS_URL;
|
||||||
|
const wsUrl = "ws://127.0.0.1:50001"
|
||||||
|
|
||||||
|
let socket = new WebSocket(wsUrl);
|
||||||
|
const emitter = new Vue({
|
||||||
|
methods: {
|
||||||
|
send(message) {
|
||||||
|
if (socket.readyState === 1) {
|
||||||
|
socket.send(JSON.stringify(message));
|
||||||
|
}
|
||||||
|
}, connect() {
|
||||||
|
socket = new WebSocket(wsUrl);
|
||||||
|
socket.onmessage = (msg) => {
|
||||||
|
this.$emit('message', msg.data);
|
||||||
|
};
|
||||||
|
socket.onerror = (err) => {
|
||||||
|
this.$emit('error', err);
|
||||||
|
};
|
||||||
|
socket.onclose = () => {
|
||||||
|
emitter.connect()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
emitter.connect();
|
||||||
|
export default emitter;
|
@@ -1,17 +1,13 @@
|
|||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
import Vuex from 'vuex'
|
import Vuex from 'vuex'
|
||||||
|
import ws from './ws'
|
||||||
|
|
||||||
Vue.use(Vuex)
|
Vue.use(Vuex)
|
||||||
|
|
||||||
export default new Vuex.Store({
|
export default new Vuex.Store({
|
||||||
state: {
|
state: {},
|
||||||
},
|
getters: {},
|
||||||
getters: {
|
mutations: {},
|
||||||
},
|
actions: {},
|
||||||
mutations: {
|
modules: {ws}
|
||||||
},
|
|
||||||
actions: {
|
|
||||||
},
|
|
||||||
modules: {
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
21
src/ui/src/store/ws.js
Normal file
21
src/ui/src/store/ws.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
export const state = {
|
||||||
|
wsRes: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const actions = {};
|
||||||
|
|
||||||
|
export const mutations = {
|
||||||
|
setWsRes(newState, payload) {
|
||||||
|
state.wsRes = payload;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getters = {};
|
||||||
|
|
||||||
|
export default {
|
||||||
|
state,
|
||||||
|
actions,
|
||||||
|
mutations,
|
||||||
|
getters,
|
||||||
|
namespaced: true
|
||||||
|
}
|
@@ -28,6 +28,8 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import {mapState} from 'vuex'
|
||||||
|
import Socket from '@/communication/socket'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'Home',
|
name: 'Home',
|
||||||
@@ -62,9 +64,21 @@ export default {
|
|||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
openDir() {
|
openDir() {
|
||||||
|
Socket.send({
|
||||||
|
type: 'openDir',
|
||||||
|
data: {},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {}
|
computed: {
|
||||||
|
...mapState({
|
||||||
|
wsRes: state => state.ws.wsRes
|
||||||
|
})
|
||||||
|
},
|
||||||
|
watch: {
|
||||||
|
wsRes(val) {
|
||||||
|
console.log('echo from server: ', val)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
Reference in New Issue
Block a user