Vuex和Websockets

Dan*_*SMc 10 websocket node.js electron vuex vuejs2

所以目前我正在与VueJS 2合作,我对它很新.现在我得到了一些其他人的帮助,但我仍然被困住了.

这是我想要实现的目标(例如 - 与我想要的密切相关):

  1. 我有一个侦听WebSockets的NodeJS应用程序.应用程序通过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

<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>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

其他所有东西都只是你看到的锅炉板,所以如果有人愿意帮助我并给我一些解释这个或其他什么的读物提示?因为我遗憾地找不到太多关于它的信息.

Ber*_*ert 11

我有一个电子应用程序,使用Vue和websocket作为信息,这是我如何设置我的.

我有一个商店定义,实际上也创建和设置websocket.

Store.js

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
Run Code Online (Sandbox Code Playgroud)

Renderer.js

import store from "./store"

new Vue({
    data:{
        store
    }
})
Run Code Online (Sandbox Code Playgroud)

这会将我的商店暴露在我的Vue的根级别,并允许我将数据传递给组件,或者你有什么.商店管理来自websocket的所有传入信息.

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

mySocket.on("message", msg => vuexStore.dispatch("onSocketMessage", msg))
Run Code Online (Sandbox Code Playgroud)

并像往常一样设置Vue和组件以与Vuex一起使用.

  • 在vuex文档中还有一个websocket插件示例:https://vuex.vuejs.org/en/plugins.html#committing-mutations-inside-plugins (6认同)