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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料