我正在尝试使用 Vue-Native 实现基于 Websocket 客户端的 android 应用程序。
Websocket 服务器正在我的本地主机(笔记本电脑)上运行。我无法让客户端连接到我的服务器。我不断收到错误 Websocket 未定义。
我被这个问题困扰了将近一个星期。任何指导或帮助表示赞赏。
- - - - - - - - - - - 代码 - - - - - - - - - - - - - - - App.vue 文件
<template>
<Page id="app">
<ActionBar title="VUe JS Websocket"></ActionBar>
<StackLayout>
<Label class="body m-20" :text="message" textWrap="true"></Label>
<Button class="btn btn-primary" @tap="sendMessage('hello')">Send Message</Button>
<Button class="btn btn-primary" @tap="createAndSend">Connect</Button>
</StackLayout>
</Page>
</template>
<script>
import Vue from 'vue'
import { mapActions, mapMutations } from 'vuex';
import { mapGetters } from 'vuex';
//import * as WebSocket from 'ws';
import VueNativeSock from 'vue-native-websocket'
//var NAME = eventBus.$on('user-name', (usrName) => usrName);
//var WSLink = 'ws://pm.tada.team/ws?name=' + NAME;
let WSLink = "ws://192.161.215.238:8000"
//const WebSocket = require('ws');
export default {
name: 'App',
data: function () {
return {
connection: null,
abc: null,
message: 'nothaaadf222ing'
}
},
methods: {
createAndSend() {
Vue.use(VueNativeSock, 'wss://echo.websocket.org/', { format: 'json' });
this.$socket.onopen = () => {
console.log('WebSocket opened');
this.message = 'Hi How Are'
// registering listeners
this.$socket.onmessage = (data) => console.log('Received data:', data.data);
// sending data
console.log('Sending data');
this.$socket.sendObj({awesome: 'data'});
}
},
},
created: function () {
console.log("Started")
this.message = this.$store.getters.messageVal;
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
- - - - - - - - - - - 代码 - - - - - - - - - - - - - - --
提前致谢。