client: Begin store implem, display last 20 messages

parent 24645257
<template> <template>
<div id="app"> <div id="app">
<HelloWorld msg="Salut Menteur !"/> <HelloWorld />
<Sockets/> <Sockets/>
<div id="game"> <div id="game">
<label for="messages"> Choose a message:</label> <label for="messages"> Choose a message:</label>
...@@ -18,35 +18,15 @@ ...@@ -18,35 +18,15 @@
<script> <script>
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex'
import 'es6-promise/auto' // Needed for Promise polyfill import 'es6-promise/auto' // Needed for Promise polyfill
import VueSocketIO from 'vue-socket.io' import VueSocketIO from 'vue-socket.io'
import Vuex, {mapGetters, mapMutations} from 'vuex';
import HelloWorld from "./components/HelloWorld"; import HelloWorld from "./components/HelloWorld";
import Sockets from "./Sockets"; import Sockets from "./Sockets";
import {store} from './vuex-store'
Vue.use(Vuex); Vue.use(Vuex);
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment(state) {
state.count++
},
decrement(state) {
state.count++
}
}
// TODO: Use actions for VueX-SocketIO integration?
// actions: {
// "<ACTION_PREFIX><EVENT_NAME>"() {
// // do something
// }
// }
});
Vue.use(new VueSocketIO({ Vue.use(new VueSocketIO({
debug: true, debug: true,
connection: 'http://localhost:9042', connection: 'http://localhost:9042',
...@@ -61,6 +41,7 @@ ...@@ -61,6 +41,7 @@
export default { export default {
name: "App", name: "App",
store: store,
components: { components: {
HelloWorld, HelloWorld,
Sockets Sockets
...@@ -70,11 +51,16 @@ ...@@ -70,11 +51,16 @@
message: "WAITING" message: "WAITING"
} }
}, },
computed: {
...mapGetters(['isConnected', 'name', 'socketMessage'])
},
methods: { methods: {
sendMessage: function(message) { sendMessage: function (message) {
console.log("User wants to send", message); console.log("User wants to send", message);
this.$socket.emit("message", message); this.$socket.emit("message", message);
} },
...mapMutations(['setName']),
// ...mapActions(['setNumberToRemoteValue']),
} }
}; };
......
...@@ -2,9 +2,16 @@ ...@@ -2,9 +2,16 @@
<div> <div>
<p v-if="isConnected">We're connected to the server!</p> <p v-if="isConnected">We're connected to the server!</p>
<p v-if="!isConnected">We're not connected yet...</p> <p v-if="!isConnected">We're not connected yet...</p>
<div v-if="socketMessage != null">
<p>Message from server: "{{socketMessage}}"</p> <div v-if="socketMessages.length >= 1">
<p>Messages from server"</p>
<ul id="messages">
<li v-for="message in socketMessages.slice(-20)" :key="message">
{{ message }}
</li>
</ul>
</div> </div>
<button @click="pingServer()">Ping Server</button> <button @click="pingServer()">Ping Server</button>
<button @click="logOn()">LogOn</button> <button @click="logOn()">LogOn</button>
<button @click="logOff()">LogOff</button> <button @click="logOff()">LogOff</button>
...@@ -16,7 +23,7 @@ ...@@ -16,7 +23,7 @@
data() { data() {
return { return {
isConnected: false, isConnected: false,
socketMessage: null socketMessages: []
} }
}, },
...@@ -29,13 +36,11 @@ ...@@ -29,13 +36,11 @@
disconnect() { disconnect() {
this.isConnected = false; this.isConnected = false;
console.log("logoff"); console.log("logoff");
}, this.socketMessages = [];
customEmit: function (data) {
console.log('this method was fired by the socket server. Data:', data);
}, },
// Fired when the server sends something on the "messageChannel" channel. // Fired when the server sends something on the "messageChannel" channel.
messageChannel(data) { messageChannel(data) {
this.socketMessage = data; this.socketMessages.push(data);
console.log("Message received:", data, "!"); console.log("Message received:", data, "!");
} }
}, },
...@@ -58,7 +63,7 @@ ...@@ -58,7 +63,7 @@
}, },
logOff() { logOff() {
this.$socket.ondisconnect(); this.$socket.ondisconnect();
this.socketMessage = null; this.socketMessage = [];
} }
} }
} }
......
<template> <template>
<div class="hello"> <div class="hello">
<h1>{{ msg }}</h1> <h1>Salut {{ this.printName() }} !</h1>
</div> </div>
</template> </template>
...@@ -8,7 +8,14 @@ ...@@ -8,7 +8,14 @@
export default { export default {
name: "HelloWorld", name: "HelloWorld",
props: { props: {
msg: String name: String
},
methods: {
printName() {
let name = this.$store.name;
console.log("Saying hello to ", name);
return name
}
} }
}; };
</script> </script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment