Socket.io Web套接字连接握手错误

时间:2019-03-21 12:25:59

标签: node.js socket.io ubuntu-16.04 digital-ocean

我基于一个教程构建了我的第一个聊天应用程序,它在我的本地计算机上运行良好。我将其部署到了 digitalocean 子域,尽管该应用程序正在运行...我在控制台中收到此错误,并疯狂地将其删除:

index.js:83 WebSocket connection to 'wss://chat.webfacemedia.com/socket.io/?EIO=3&transport=websocket&sid=-Pa3Le2Vnn1MZnbGAAAj' failed: Error during WebSocket handshake: Unexpected response code: 400

我的服务器代码:

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
users=[];
connections =[];

server.listen(process.env.PORT || 3000,'localhost');
console.log('Server Running');
app.get('/',function(req, res){
   res.sendFile(__dirname + '/index.html');
});

io.sockets.on('connection', function(socket){
  connections.push(socket);
  console.log('Connected: %s sockets connected!',connections.length);

  //Disconnect
  socket.on('disconnect',function(data){
      users.splice(users.indexOf(socket.username),1);
      connections.splice(connections.indexOf(socket),1);
      updateUsernames();
      console.log('Disconnected: %s sockets 
       connected!',connections.length);
  });

  socket.on("send message",function(data){
      io.sockets.emit("new message", { msg: data , user: 
       socket.username});
  });

  socket.on("new user", function (data, callback){
      callback(true);
      socket.username = data;
      console.log(data);
      users.push(socket.username);
      updateUsernames();
  });

   function updateUsernames(){
      io.sockets.emit("get users",users);
   }
})

客户代码:

$(function(){
    var socket = io.connect();
    $messageForm = $("#messageForm");
    $message = $("#message");
    $chat = $("#chat");
    $messageArea = $("#messageArea");
    $userFormArea = $("#userFormArea");
    $userForm = $("#userForm");
    $users = $("#users");
    $username = $("#username");

    $messageForm.submit(function(e){
        e.preventDefault();
        socket.emit('send message', $message.val());
        $message.val('');
    });
    socket.on('new message',function(data){
        $chat.append("<div class='card bg-light'><strong>"+data.user+"</strong>"+data.msg+"</div>");
    });

    $userForm.submit(function(e){
        e.preventDefault();
        socket.emit('new user', $username.val(),function(data){
            if(data){
                $userFormArea.hide();
                $messageArea.css('display','flex');
            }
        });
        $username.val('');
    });

    socket.on('get users', function(data){
        var html = '';
        for(var i = 0; i < data.length; i++){
            html += '<li class="list-group-item">'+data[i]+'</li>';
        }
        $users.html(html);
    });
})

Ubuntu Conf文件

<virtualhost *:80>
    ServerName chat.webfacemedia.com
    ProxyPreserveHost on
    ProxyPass / http://localhost:3000
    ProxyPassReverse / http://localhost:3000
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =chat.webfacemedia.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI}[END,NE,R=permanent]
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
    RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
    RewriteRule /socket.io/(.*) ws://localhost:3000/socket.io/$1 [P]
</virtualhost>

我在https上运行子域,并且在运行聊天时,仍然在控制台中收到错误消息。访问chat.webfacemedia.com并检查检查员。谷歌搜索答案只会导致我进入nginx论坛,而我没有使用nginx。有帮助吗?

0 个答案:

没有答案