Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Node.JS中向客户端发送通知_Javascript_Node.js_Service Worker - Fatal编程技术网

Javascript 在Node.JS中向客户端发送通知

Javascript 在Node.JS中向客户端发送通知,javascript,node.js,service-worker,Javascript,Node.js,Service Worker,在NodeJS应用程序中,我希望我的服务器能够在某些特定情况下向客户端发送通知。在网上搜索这个主题,我找到了我所需要的服务人员;但我不能完全肯定 这就是原因。当我看各种教程和视频时,似乎通知总是来自某个客户端。但这不是我想要的。应该由服务器来决定何时以及发送什么作为通知,而不是由某个客户端来决定。我的问题来了: 我是否误解了使用服务人员的方式 这是让我的服务器向客户端发送通知的正确路径吗 如果最后一个问题的答案是否定的,那么正确的方法是什么?答案是否定的 退房。它将使您能够在客户端上接收实时通知

在NodeJS应用程序中,我希望我的服务器能够在某些特定情况下向客户端发送通知。在网上搜索这个主题,我找到了我所需要的服务人员;但我不能完全肯定

这就是原因。当我看各种教程和视频时,似乎通知总是来自某个客户端。但这不是我想要的。应该由服务器来决定何时以及发送什么作为通知,而不是由某个客户端来决定。我的问题来了:

  • 我是否误解了使用服务人员的方式
  • 这是让我的服务器向客户端发送通知的正确路径吗
  • 如果最后一个问题的答案是否定的,那么正确的方法是什么?

    答案是否定的


    退房。它将使您能够在客户端上接收实时通知,并且您可以对它们进行完全的服务器控制。

    使用twilio库来管理通知作业怎么样。

    就像Dimitar之前写的那样,您可以使用socket.io从服务器向客户端发送消息,反之亦然。这可以简单地做到:

    //Server sent message:
    io.on('connection', (socket) => {
      socket.emit('notificationToClient', 'Message'); //message sent from server to client 
      });
    });
    
    //Client receives the message:
    const socket = io.connect('http://localhost:3000');
      socket.on('notificationToClient', (data) => { //received message 
        console.log(data);
      });
    
    websocket API比socket.io稍微少一点混乱,但在我看来,我会选择socket.io,因为它已经处理了很多繁重的工作,比如重新连接、自定义名称空间、房间等。我在socket.io上有几个精彩的课程

    请参见此处的socket.io文档:

    更新日期:2020年5月9日:

    Michel-既然您询问了如何在代码中实现Socket.io,我就给您举一个例子,说明我在最近的项目中是如何实现这项技术的。我当时正在开发一个web应用程序,它可以连接到这些系统,并能够在任何远程位置启用/解除这些系统,以及接收传感器输入(蜂鸣信号)。后端是Node.js,用户界面是React

    //client side code
    
    import React, { Component, Suspense } from 'react';
    import panels from "./panels.js"
    
    const socketConnection = io("http://localhost:5000"); //Socket.io
    
    function App() {
    
        function handleClick(panel) {
            socketConnection.emit("panelData", panel); //Socket.io - sends the connection data for the ELK alarm panel to the express server
    
            socketConnection.on('data', (data) => { //Socket.io - receives data from the server.
             //does something with the data
            })
        }
    }
    
    //server side code
    
    const express = require('express');
    const elkClient = require('elk-client');
    
    const app = express();
    
    const server = app.listen('5000', () => {
      console.log('------Server Running on 5000---------');
    });
    
    let io = new sio(server);
    
    io.on("connection", (socket) => { //boilerplate code - establishes the "handshake" with the client.
     
        socket.on("panelData", async (msg) => { //receives the ELK connection parameters from the client. See client side code
        const client = new ElkClient({
                    connection: { 
                      site: msg.name,
                      host: msg.host,
                    }
                  })
            await client.connect();
    
        client.on("message", (elkMessage) => { // This is NOT Socket.io - server is listening for emitted data from the alarm system - beam breaks, alarms, arm/disarm etc. [See event emitter][2]
                        if(elkMessage.messageType == 'A' && elkMessage.subMessageType == 'S') {
                          const armingStatusReport = {elkMessage};
                          socket.emit("data", armingStatusReport); //Socket.io - emits the received data to the client.
                        } 
                })
    
    我试图简化上面的代码以抓住要点。服务器正在使用从客户端接收的连接参数连接到远程报警系统。然后,服务器使用client.on()等待并侦听传入数据(事件发射器-而不是socket.io)。在接收数据时,我使用Socket.io将接收到的数据通过Socket.emit()发送回客户端;由于报警系统工作的性质,数据是由事件驱动发送的,因此fetch api并不真正符合要求,但socket.io(或websockets)符合要求


    如果我还能帮忙,请告诉我。在最近的这个项目中,我花了几个月的时间广泛地处理socket.io,包括名称空间、房间等。自从我实现集群以来,我还必须使用socket.io为我的服务器创建一个用于性能管理的实时监控应用程序。请随时与我联系

    我现在正在看twilio图书馆。您的回答是否意味着我的方法是错误的?我现在实际上正在查看socket.io。我使用npm将其安装到我的应用程序中。我喜欢你在回答中提出解决方案的简单方式,但我还没有找到在我自己的应用程序中实现它的正确途径。我有一个问题:您的代码中的“服务器发送消息:…”部分是否需要在我的服务器代码中的某个位置?Michel-我更新了上面的帖子,并添加了另一个代码示例,以概述我在最近的项目中如何使用socket.io。希望这对你有帮助。如果您有任何后续问题,请随时联系我们。我很乐意提供帮助。这可以在http环境中完成吗?在http环境中,其他端点实现标准的req res模式?