发布于 2017-05-26 15:26:19
不,你不需要中间人。但是,如果通过通道进行大量更新,您可以更好地使用Vuex并使用套接字数据向它提供信息。然后,如果一切都连接正确,您的Vue应用程序将只是视图层的反应(没有双关意)的变化。
Django通道只是队列(先到先出)。您将需要发送到前端的任何数据传递到频道。所有数据都被序列化并传递到队列中。通道处于工作模式,一旦收到消息(带有数据),它就会尝试在它自己的通道上发出消息。
如何在Vue中获取这些数据?
我所做的是陷害Vuex。然后我制作了一个名为createWebSockets.js的Vuex插件。当您浏览Vuex和Vuex插件的文档时,您将看到插件可以访问Vuex commit方法。在上述插件中,我向服务器上运行的通道打开了套接字,每当新消息出现时,我就按Vuex中的数据,而我的Vue应用程序只是对这些变化做出反应。
如果你需要更多的帮助我可能会在某个地方找到它。
最好的
编辑
因此,在你熟悉Vuex并将其添加到应用程序之后,您可以这样做:
//插件代码
// importing from node_modules -> you have to install it
// through npm or yarn
import io from 'socket.io-client'
// opening a socket to an IP. Mind that I've put an
// example IP here yours will be an IP belonging to the
// server or 127.0.0.1 if you're working locally
const socket = io('127.0.0.1:4000')
// this is a vuex plugin that takes the store (vuex store)
// object as its parametar
export default function createWebSockets(socket) {
// it returns a function to which we passed store object
return (store) => {
// this is your channel name on which you want to
// listen to emits from back-end
const channel_name = 'whatever-you-called-it'
// this opens a listener to channel you named on line above
socket.on('channel_name', (data) => { //
// and this is the store part where you
// just update your data with data received from socket
store.commit('YOUR_VUEX_MUTATION', data)
})
// you can add multiple socket.on statements if you have more than one channel
}
}这就是通过套接字更新Vuex的方法。
希望能帮上忙。
https://stackoverflow.com/questions/44186740
复制相似问题