Initial Websockets, Rooms & Players done
This commit is contained in:
61
src/components/Game.vue
Normal file
61
src/components/Game.vue
Normal file
@@ -0,0 +1,61 @@
|
||||
<template>
|
||||
<div>
|
||||
<button v-on:click="sendMessage()">Send Message</button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
connection: null,
|
||||
message: {},
|
||||
};
|
||||
},
|
||||
created() {
|
||||
console.log("Starting connection to websocet server");
|
||||
console.log(this.$route.params.id);
|
||||
this.connection = new WebSocket(
|
||||
"ws://192.168.1.244:4000/" + this.$route.params.id
|
||||
);
|
||||
|
||||
this.connection.onopen = function (event) {
|
||||
console.log(event);
|
||||
console.log("Successfully connected to the echo WebSocket Server");
|
||||
};
|
||||
|
||||
this.connection.onmessage = function (event) {
|
||||
console.log(event); // When we recieve a message
|
||||
};
|
||||
},
|
||||
mounted() {},
|
||||
methods: {
|
||||
sendMessage() {
|
||||
console.log(this.connection);
|
||||
this.message.playerId = this.getCookie("playerId");
|
||||
this.message.gameId = this.$route.params.id;
|
||||
console.log(this.message);
|
||||
let str = JSON.stringify(this.message);
|
||||
this.connection.send(str);
|
||||
},
|
||||
getCookie(cname) {
|
||||
var name = cname + "=";
|
||||
var decodedCookie = decodeURIComponent(document.cookie);
|
||||
var ca = decodedCookie.split(";");
|
||||
for (var i = 0; i < ca.length; i++) {
|
||||
var c = ca[i];
|
||||
while (c.charAt(0) == " ") {
|
||||
c = c.substring(1);
|
||||
}
|
||||
if (c.indexOf(name) == 0) {
|
||||
return c.substring(name.length, c.length);
|
||||
}
|
||||
}
|
||||
return "";
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
<style>
|
||||
</style>
|
||||
@@ -1,58 +0,0 @@
|
||||
<template>
|
||||
<div class="hello">
|
||||
<h1>{{ msg }}</h1>
|
||||
<p>
|
||||
For a guide and recipes on how to configure / customize this project,<br>
|
||||
check out the
|
||||
<a href="https://cli.vuejs.org" target="_blank" rel="noopener">vue-cli documentation</a>.
|
||||
</p>
|
||||
<h3>Installed CLI Plugins</h3>
|
||||
<ul>
|
||||
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-babel" target="_blank" rel="noopener">babel</a></li>
|
||||
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint" target="_blank" rel="noopener">eslint</a></li>
|
||||
</ul>
|
||||
<h3>Essential Links</h3>
|
||||
<ul>
|
||||
<li><a href="https://vuejs.org" target="_blank" rel="noopener">Core Docs</a></li>
|
||||
<li><a href="https://forum.vuejs.org" target="_blank" rel="noopener">Forum</a></li>
|
||||
<li><a href="https://chat.vuejs.org" target="_blank" rel="noopener">Community Chat</a></li>
|
||||
<li><a href="https://twitter.com/vuejs" target="_blank" rel="noopener">Twitter</a></li>
|
||||
<li><a href="https://news.vuejs.org" target="_blank" rel="noopener">News</a></li>
|
||||
</ul>
|
||||
<h3>Ecosystem</h3>
|
||||
<ul>
|
||||
<li><a href="https://router.vuejs.org" target="_blank" rel="noopener">vue-router</a></li>
|
||||
<li><a href="https://vuex.vuejs.org" target="_blank" rel="noopener">vuex</a></li>
|
||||
<li><a href="https://github.com/vuejs/vue-devtools#vue-devtools" target="_blank" rel="noopener">vue-devtools</a></li>
|
||||
<li><a href="https://vue-loader.vuejs.org" target="_blank" rel="noopener">vue-loader</a></li>
|
||||
<li><a href="https://github.com/vuejs/awesome-vue" target="_blank" rel="noopener">awesome-vue</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'HelloWorld',
|
||||
props: {
|
||||
msg: String
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<!-- Add "scoped" attribute to limit CSS to this component only -->
|
||||
<style scoped>
|
||||
h3 {
|
||||
margin: 40px 0 0;
|
||||
}
|
||||
ul {
|
||||
list-style-type: none;
|
||||
padding: 0;
|
||||
}
|
||||
li {
|
||||
display: inline-block;
|
||||
margin: 0 10px;
|
||||
}
|
||||
a {
|
||||
color: #42b983;
|
||||
}
|
||||
</style>
|
||||
44
src/components/Home.vue
Normal file
44
src/components/Home.vue
Normal file
@@ -0,0 +1,44 @@
|
||||
<template>
|
||||
<div class="hello">
|
||||
Create Room: <button v-on:click="createRoom" style="border: solid">Create Room</button>
|
||||
<br>
|
||||
Join Room: <input type="text" v-model="room.id" style="border: solid"> <button v-on:click="joinRoom" style="border: solid">Join Room</button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
room: {},
|
||||
};
|
||||
},
|
||||
methods: {
|
||||
createRoom() {
|
||||
this.axios.get("http://localhost:4000/rooms/add").then((res) => {
|
||||
let id = res.data._id;
|
||||
let playerId = res.data.playerId;
|
||||
this.setCookie("playerId", playerId, 1);
|
||||
this.$router.push({path: `game/${id}`});
|
||||
});
|
||||
},
|
||||
joinRoom() {
|
||||
console.log(this.room);
|
||||
this.axios.post("http://localhost:4000/rooms/join", this.room).then((res) => {
|
||||
let playerId = res.data._id;
|
||||
this.setCookie("playerId", playerId, 1);
|
||||
this.$router.push({path: `game/${this.room.id}`});
|
||||
});
|
||||
},
|
||||
setCookie(cname, cvalue, exdays) {
|
||||
var d = new Date();
|
||||
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
|
||||
var expires = "expires=" + d.toUTCString();
|
||||
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
</style>
|
||||
Reference in New Issue
Block a user