【问题标题】:Node and Angular socket error in browser console浏览器控制台中的节点和角度套接字错误
【发布时间】:2018-03-30 04:59:07
【问题描述】:

我正在使用 Node(server) + Angular(client) 在我的应用程序中实现套接字。

Angular bower.json 套接字组件:“angular-socket-io”: "^0.7.0","socket.io-client": "^1.7.2",

package.json 中的节点 js 套接字组件:“socket.io”:“^1.7.3”,

我在我的 chrome 浏览器控制台中看到以下 Web 套接字错误:

WebSocket 连接到 'wss://ireporter.apple.com/uitracker/socket.io/?EIO=3&transport=websocket&sid=4qBY-qoxEzUQZOvUAACb' 失败:WebSocket 握手期间出错:net::ERR_CONNECTION_RESET WrappedWebSocket @ VM43:161

这个错误可能只发生在生产环境中。不记得在本地运行应用程序时看到此错误。

也只从服务器端和客户端发布与套接字相关的代码:

Node js 服务器端代码

start.js 文件

var express = require('express');
var configure = require("./config/configure");
var logger = require("./config/components/logger")
var app = express();
var server = require('http').Server(app);
server.listen(process.env.PORT || config.port, function() {
    logger.info("Express server listening on port", config.port );
});
//Configure with all the basic middlewares and configs
configure(app,server);

configure.js 文件

var socket = require('./middleware/socket/socket.js');
module.exports = function (app,server) {
    app.use(socket(server));
}

socket.js 文件

"use strict";

var logger = require("../../components/logger");

module.exports = function(server){
    var io = require('socket.io')(server, {path: '/appname/socket.io'});
    require('./socketServer.js')(io, logger);

    return function (req, res, next) {
        req.io = io;
        next();
    };
};

socketServer.js

//export function for listening to the socket
module.exports = function(io, logger) {
    io.on('connection', function(socket) {
        socket.on('notification:update', function(data) {
            io.emit('notification:update', data);
        });
    });
};

Angular js 客户端代码:

Socket.js

MyApp.factory('Socket', function ($rootScope) {
    var socket = io.connect('' , {path: '/appname/socket.io'});
    return {
        on: function (eventName, callback) {
            socket.on(eventName, function () {
                var args = arguments;
                $rootScope.$apply(function () {
                    callback.apply(socket, args);
                });
            });
        },
        emit: function (eventName, data, callback) {
            socket.emit(eventName, data, function () {
                var args = arguments;
                $rootScope.$apply(function () {
                    if (callback) {
                        callback.apply(socket, args);
                    }
                });
            })
        }
    };
});

notificationController.js

Socket.on('notification:update', function(data) {
});

-- 谁能建议如何解决控制台错误?

【问题讨论】:

    标签: angularjs node.js sockets


    【解决方案1】:

    原来在您的服务器前面还有另一个我无法控制的反向代理。请检查您的服务器设置。问题不在于代码。

    Error during WebSocket handshake: net::ERR_CONNECTION_RESET

    也可以试试这个来测试你的服务器端。

    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.2/socket.io.js"></script>
    <script>
        /*var socket = io('', {
            path: '/appname/socket.io'
        });*/
         var socket = io.connect('' , {path: '/appname/socket.io'});
    
        socket.on('notification:update', function (message) {
            console.log('notification:update ', message);
        });
    
        setTimeout(function() {
          console.log('emit demo');
          socket.emit('notification:update', 'DEMO');
        }, 1000);
    
        socket.on('connect', function (data) {
          console.log('connection');
        });
    </script>
    

    【讨论】:

      猜你喜欢
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      相关资源
      最近更新 更多