首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vuex & Websockets

Vuex & Websockets
EN

Stack Overflow用户
提问于 2017-06-02 16:05:23
回答 1查看 17.3K关注 0票数 11

因此,目前我正在使用VueJS 2,而且我对它非常陌生。现在我得到了一些其他人的帮助,但我仍然被困住了。

以下是我想要达到的目标(例如--与我想要的东西紧密相连):

  1. 我有一个NodeJS应用程序可以监听WebSockets。应用程序通过WebSocket侦听连接,并将接受JSON数据,其中包含命令,然后是包含该命令所需内容的数据对象。

例如,命令可以是登录,数据可以是用户名和密码。然后,NodeJS应用程序上的登录函数将获取该数据,做它所需的操作,然后通过套接字返回它,无论它是否成功,并且可能包括一个ID和一些用户信息,以便Vuex拾取并放置在它的状态中,以便应用程序的前端获取/使用。

目前我正在使用这个锅炉板:https://github.com/SimulatedGREG/electron-vue

因为我想使用Vue和Vuex来管理我的应用程序,然后使用WebSockets来管理数据服务器之间的数据,这对我的学习曲线非常有用。

因此,如果您查看我在app/src/renderer/中发送的链接(这是vue和vuex的主要代码所在)。

我的一个朋友为我添加了下面的代码作为一个例子,我被困在把它作为动作和变异的vuex。他在一个vue组件中完成了这一切,所以我很难理解它是如何与vuex一起工作的。因为我希望能够从应用程序的任何位置访问(例如) loginUser操作(使用多个页面/视图的路由)。

所以在MyApp/app/src/renderer/components/LandingPageView.vue

代码语言:javascript
复制
<template>
  <div>
    <img src="./LandingPageView/assets/logo.png" alt="electron-vue">
    <h1>Welcome.</h1>
    <current-page></current-page>
    <websocket-output></websocket-output>
    <versions></versions>
    <links></links>
  </div>
</template>

<script>
  import CurrentPage from './LandingPageView/CurrentPage'
  import Links from './LandingPageView/Links'
  import Versions from './LandingPageView/Versions'
  import WebsocketOutput from './LandingPageView/WebsocketOutput'
  export default {
    components: {
      CurrentPage,
      Links,
      Versions,
      WebsocketOutput
    },
    name: 'landing-page'
  }
</script>

<style scoped>
  img {
    margin-top: -25px;
    width: 450px;
  }
</style>

这是更新后的文件,下面是MyApp/app/src/renderer/components/LandingPageView/WebsocketOutput.vue的代码

代码语言:javascript
复制
<template>
  <div>
    <h2>Socket output:</h2>
    <p v-text="socket_out"></p>
  </div>
</template>

<script>
  var ws = require("nodejs-websocket")

  export default {
    data () {
      return {
        socket_out: "connecting to the websocket server..."
      }
    },
    mounted () {
      const parent = this
      var connection = ws.connect("ws://dannysmc.com:9999", {}, function (conn) {})
      connection.on("text", function (text) {
        console.log('Text received: ' + text)
        parent.socket_out = text
      })
      connection.on("connect", function () {
        connection.send('yo')
      })
    },
    created () {
      // Set $route values that are not preset during unit testing
      if (process.env.NODE_ENV === 'testing') {
        this.$route = {
          name: 'websocket-output',
          path: '/websocket-output'
        }
      }
    }
  }
</script>

<style scoped>
  code {
    background-color: rgba(46, 56, 76, .5);
    border-radius: 3px;
    color: #fff;
    font-weight: bold;
    padding: 3px 6px;
    margin: 0 3px;
    vertical-align: bottom;
  }

  p {
    line-height: 24px;
    color: red;
  }
</style>

其他的一切只是你看到的锅炉板,所以如果有人愿意帮助我,给我一些阅读的提示,解释这个或其他什么?不幸的是我找不到很多关于它的信息。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-02 19:31:55

我有一个电子应用程序,它使用Vue和websocket来获取信息,下面是我如何设置的。

我已经定义了一个商店,它实际上也创建和设置了websocket。

Store.js

代码语言:javascript
复制
const socket = require("socket-library") // Take your pick of socket libs

const mySocket = new socket(...)
mySocket.on("message", message => store.handleMessage(message))
...other handlers...

const store = {
    handleMessage(message){
        // do things with the message
    }
}

export default store

Renderer.js

代码语言:javascript
复制
import store from "./store"

new Vue({
    data:{
        store
    }
})

这将在Vue的根级公开我的存储,并允许我将数据传递给组件或您所拥有的组件。存储管理来自websocket的所有传入信息。

如果您想使用Vuex,您基本上可以做同样的事情,Vuex将是您的商店,当消息通过套接字传入时,您只需将它们传递给Vuex。

代码语言:javascript
复制
mySocket.on("message", msg => vuexStore.dispatch("onSocketMessage", msg))

并设置您的Vue和组件来与Vuex一起工作,就像您通常所做的那样。

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44333164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档