简体   繁体   English

本地 Node.js 服务器接收和发送消息到网站上的组件

[英]Local Node.js Server Recieve and Send messages to component on website

I want to run a local Node.js server, or listen for events and then run other Node functions, but also emit messages too.我想运行一个本地 Node.js 服务器,或者监听事件然后运行其他节点功能,但也发出消息。

So, for example lets say within my CMS I have a button labeled Start Watch .因此,例如,假设在我的 CMS 中,我有一个标记为Start Watch的按钮。 If I click this I want to emit and even using Socket.io which my local Node script on the machine will see, and then run some more code, in this case it would be gulp watch .如果我点击这个我想发出,甚至使用Socket.io ,我在机器上的本地节点脚本会看到,然后运行更多代码,在这种情况下它将是gulp watch

Then in the other direction during the watch, if files are uploaded I want to be able to emit this to my CMS to update the user visually that a file was uploads or something.然后在观看期间的另一个方向,如果文件被上传,我希望能够将其发送到我的 CMS,以直观地更新用户文件已上传或其他内容。

This is completely wrong I think but this is my attempt, but doesn't work!#我认为这是完全错误的,但这是我的尝试,但不起作用!#

Vue Component on CMS CMS 上的 Vue 组件

<template>
    <h2>Chat Testing</h2>
</template>
<script>
export default {
    mounted() {
        let socket = require('socket.io');
        socket.emit('connection', []);
    }
}
</script>

Local script on laptop:笔记本电脑上的本地脚本:

let app = require('express')();
let http = require('http').createServer(app);
let io = require('socket.io')(http);

io.on('connection', (socket) => {
    console.log('a user connected');
});

http.listen(3000, () => {
    console.log('listening on *:3000');
});

Use the package socket.io-client on the client.在客户端使用 package socket.io-client client。

First you need to connect to the server.首先,您需要连接到服务器。

Client API客户端 API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM