如何在路由上侦听socket.io事件以重定向到Express中的另一个路由?

时间:2019-06-23 20:29:29

标签: node.js express socket.io express-router

我正在尝试为我的“最后一年”项目(在线医疗管理系统)复制Web whatsapp QRCode身份验证。在这个长话短说的系统中,医生仅应在患者允许的情况下查看患者的处方,并且当他扫描由其应用程序发送的authStatus套接字事件上的医生配置文件生成的QRcode时,应给予许可这将使服务器能够重定向到患者记录路径。

服务器套接字设置如下:

router.get('/patientinfoqrcode',redirectRoutes.redirectLoginDoctor,function(req,res){
    let usernameQR = req.query.usernameQR
    let message='';
    io.on("connection",function(socket){
        console.log("The user has been connected")

        io.on("disconnect",function(){
            console.log("The user has been disconnected")
        })

        socket.on("Auth",function(data){
            if(data.authStatus=='authorized'){
                res.redirect(`/dashboard?usernamePatient=${usernameQR}`)
            }
        });

    })

    res.render('doctor/patientinfoqrcode',{usernameQR})
});

原生脚本应用设置如下:

const socketio = SocketIO.connect('https://secret-falls-76814.herokuapp.com/patientinfoqrcode'
    , options);

export function scanQR(args) {
    barcodescanner.scan({
    formats: "QR_CODE",   // Pass in of you want to restrict scanning to certain type
    message: "Use the volume buttons for extra light", // Android only, default is 'Place a barcode inside the viewfinder rectangle to scan it.'
    showFlipCameraButton: true,   // default false
    preferFrontCamera: false,     // default false
    showTorchButton: true,        // default false
    beepOnScan: true,             // Play or Suppress beep on scan (default true)
    torchOn: false,               // launch with the flashlight on (default false)
    closeCallback: function () { console.log("Scanner closed"); }, // invoked when the scanner was closed (success or abort)
    resultDisplayDuration: 500,   // Android only, default 1500 (ms), set to 0 to disable echoing the scanned text
    orientation: "landscape",     // Android only, optionally lock the orientation to either "portrait" or "landscape"
    openSettingsIfPermissionWasPreviouslyDenied: true // On iOS you can send the user to the settings app if access was previously denied
  }).then(
      function(result) {
        console.log("Scan format: " + result.format);
        console.log("Scan text:   " + result.text);
        const button = args.object;
        button.text = result.text;
            socketio.emit('Auth',{
                authStatus:'authorized'
            });

      },
      function(error) {
        console.log("No scan: " + error);
      }
  );
}

它不像预期的那样工作。我认为唯一的问题是,侦听该事件的socket.io在/ Patientinfoqrcode路由内,并且该路由在加载时立即执行其中的所有功能。然后,即使在router.get()主体内发生了某些事情,也不会发生任何事情,因为它已经渲染了文档。服务器整个项目都已上传到heroku

0 个答案:

没有答案