zoukankan      html  css  js  c++  java
  • electron 运行http服务,http服务与socket同端口

    1. electron/main.js

    // main.js页面最下面
    
    // server是 express 脚手架生成的目,bin/www是启动/监听http服务器的代码.
    // 把bin/www的www文件后缀加上js,这样可以自动引入
    const {socket} =  require('./server/bin/www'); 

    2.electron/server/bin/www.js,除去引入的代码,其他代码放入到导出的socket方法内

    #!/usr/bin/env node
    
    /**
     * Module dependencies.
     */
    
    var app = require('../app');
    var debug = require('debug')('scoket:server');
    var http = require('http');
    module.exports = {
      socket:function () {
    
        /**
         * Get port from environment and store in Express.
         */
      
        var port = normalizePort(process.env.PORT || '3000');
        app.set('port', port);
      
        /**
         * Create HTTP server.
         */
      
        var server = http.createServer(app);
      
        /**
         * socket
         */
        const io = require('socket.io')(server);
        io.on('connection', (socket) => {
          console.log(socket);
          socket.on('chat message', msg => {
            console.log(msg);
            io.emit('chat message', msg);
          });
        });
        /**
         * Listen on provided port, on all network interfaces.
         */
      
        server.listen(port, () => {
          console.log(`server running at http://localhost:${port}/`);
          console.log(`Socket.IO running at http://localhost:${port}/`);
        });
        server.on('error', onError);
        server.on('listening', onListening);
      
        /**
         * Normalize a port into a number, string, or false.
         */
      
        function normalizePort(val) {
          var port = parseInt(val, 10);
      
          if (isNaN(port)) {
            // named pipe
            return val;
          }
      
          if (port >= 0) {
            // port number
            return port;
          }
      
          return false;
        }
      
        /**
         * Event listener for HTTP server "error" event.
         */
      
        function onError(error) {
          if (error.syscall !== 'listen') {
            throw error;
          }
      
          var bind = typeof port === 'string'
            ? 'Pipe ' + port
            : 'Port ' + port;
      
          // handle specific listen errors with friendly messages
          switch (error.code) {
            case 'EACCES':
              console.error(bind + ' requires elevated privileges');
              process.exit(1);
              break;
            case 'EADDRINUSE':
              console.error(bind + ' is already in use');
              process.exit(1);
              break;
            default:
              throw error;
          }
        }
      
        /**
         * Event listener for HTTP server "listening" event.
         */
      
        function onListening() {
          var addr = server.address();
          var bind = typeof addr === 'string'
            ? 'pipe ' + addr
            : 'port ' + addr.port;
          debug('Listening on ' + bind);
        }
      }
    };

     参考:https://blog.csdn.net/enmengxu/article/details/87373979

  • 相关阅读:
    Python range 函数 Python零基础入门教程
    Python eval 与 exec 函数的区别 Python零基础入门教程
    Python callable 函数 Python零基础入门教程
    Python bytes 函数 Python零基础入门教程
    Python ord 函数 Python零基础入门教程
    Python len函数 Python零基础入门教程
    第十二课 通过URL api拿到接送数据并做页面展示
    第十三课 axios请求数据
    网络编程学习路线计划
    erlang学习笔记本【不断更新】
  • 原文地址:https://www.cnblogs.com/xiaqiuchu/p/14282841.html
Copyright © 2011-2022 走看看