duanjuan3931 2016-11-12 05:32
浏览 158
已采纳

Socket.io js没有连接

I have a node.js server running on the port 8443. Whenever I try to run the application through the browser my socket.io keeps connecting for about 20 seconds before the url turns red at the end.

Edit 3 : This is my directory structure and now with updated files

/var/www/html/nodetest/

Inside this

/node_modules
/app.js
/index.html

Node JS is installed on server.

Here is my main app.js code (as suggested in answer) :

 var app = require('express')();
 var server = require('http').createServer(app);
 var io = require('socket.io')(server);
 var fs = require('fs');

  // Run server to listen on port 8443.
  server = app.listen(8443, () => {
   console.log('listening on *:8443');
 });

 io.listen(server);

 io.sockets.on('connection', function(socket) {
   socket.emit('message', 'this is the message emitted by server');
  });   

 app.get('/', function(req, res){
   fs.readFile(__dirname + 'index.html', function(error, data) {
      res.writeHead(200);
      res.end(data);         
   });
 });

And this is my client browser side code:

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.5.1    
 /socket.io.min.js"></script>
   <script type="text/javascript">
       var websocket;
        function onload() {
        websocket = io.connect();
        websocket.on('message',function(data) {
           console.log('Received a message from the server!',data);
        });

     };
   </script>
 </head>
 <body onload="onload()">
    <div id="divId"></div>
 </body>
 </html>

Here is an image of the error

enter image description here

Now it shows 404 Error

  • 写回答

4条回答 默认 最新

  • douzhan4522 2016-11-12 06:08
    关注

    I've modified the code as per your need. The code is working as it is showing message emitted by server after loading in browser's console as in the below image.

    Server.js:

    let express = require('express')
    let app = express();
    let http = require('http').Server(app);
    let io = require('socket.io')(http);
    
    let port = 8443;
    var fs = require('fs');
    var path = require('path');
    
    app.get('/', function (req, res) {
        fs.readFile(path.join(__dirname, 'index.html'), function (error, data) {
            res.writeHead(200);
            res.end(data);
        });
    });
    
    // Run server to listen on port 8443.
    http.listen(port, function () {
        console.log(`listning on ${port}`);
    });
    
    io.on('connection', function (socket) {
        socket.emit('message', 'this is the message emitted by server');
        socket.on('disconnect', function () {
            console.log('Client disconnected');
        });
    });
    

    index.html

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.5.1/socket.io.min.js"></script>
        <script type="text/javascript">
            var websocket;
            function onload() {
                websocket = io().connect();
                websocket.on('message', function (data) {
                    console.log('Received a message from the server!', data);
                });
    
            };
        </script>
    
    </head>
    <body onload="onload();">
    
    </body>
    </html>
    

    The out put is as below:enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题