Как решить проблему с WebSocket на node.js?

Есть код
#!/usr/bin/env node
var WebSocketServer = require('ws');

// подключенные клиенты
var clients = [];

// WebSocket-сервер на порту 3081
var webSocketServer = new WebSocketServer.Server({port: 3081});
webSocketServer.on('connection', function (ws) {

    var id = clients.push(ws);
    console.log("новое соединение " + id);

    ws.on('message', function (message) {
        console.log('получено сообщение ' + message);

        for (var key in clients)
                try {
                    clients[key].send(message);
                } catch (e) {
                    delete clients[id];
                }
    });

});


При запуске и подключении клиентов всё нормально, но вот про передаче сообщения node.js падает с такой ошибкой
новое соединение 1
новое соединение 2

/var/www/redcreepster.ru/node_modules/ws/lib/PerMessageDeflate.js:217
    callback(null, Buffer.concat(buffers));
                          ^
TypeError: Object function Buffer(subject, encoding, offset) {
  if (!(this instanceof Buffer)) {
    return new Buffer(subject, encoding, offset);
  }

  var type;

  // Are we slicing?
  if (typeof offset === 'number') {
    this.length = coerce(encoding);
    this.parent = subject;
    this.offset = offset;
  } else {
    // Find the length
    switch (type = typeof subject) {
      case 'number':
        this.length = coerce(subject);
        break;

      case 'string':
        this.length = Buffer.byteLength(subject, encoding);
        break;

      case 'object': // Assume object is an array
        this.length = coerce(subject.length);
        break;

      default:
        throw new Error('First argument needs to be a number, ' +
                        'array or string.');
    }

    if (this.length > Buffer.poolSize) {
      // Big buffer, just alloc one.
      this.parent = new SlowBuffer(this.length);
      this.offset = 0;

    } else {
      // Small buffer.
      if (!pool || pool.length - pool.used < this.length) allocPool();
      this.parent = pool;
      this.offset = pool.used;
      pool.used += this.length;
    }

    // Treat array-ish objects as a byte array.
    if (isArrayIsh(subject)) {
      for (var i = 0; i < this.length; i++) {
        this.parent[i + this.offset] = subject[i];
      }
    } else if (type == 'string') {
      // We are a string
      this.length = this.write(subject, 0, encoding);
    }
  }

  SlowBuffer.makeFastBuffer(this.parent, this, this.offset, this.length);
} has no method 'concat'
    at /var/www/redcreepster.ru/node_modules/ws/lib/PerMessageDeflate.js:217:27
    at InflateRaw.callback (zlib.js:404:13)


Установку ws проводил так
npm install ws             npm http GET http://registry.npmjs.org/ws
npm http 304 http://registry.npmjs.org/ws
npm http GET http://registry.npmjs.org/nan
npm http GET http://registry.npmjs.org/options
npm http GET http://registry.npmjs.org/ultron
npm http 304 http://registry.npmjs.org/ultron
npm http 304 http://registry.npmjs.org/nan
npm http 304 http://registry.npmjs.org/options

> ws@0.6.4 install /var/www/redcreepster.ru/node_modules/ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)

ws@0.6.4 ./node_modules/ws
├── options@0.0.6
├── ultron@1.0.1
└── nan@1.4.1
  • Вопрос задан
  • 3285 просмотров
Решения вопроса 1
@RedCreepster Автор вопроса
Решение было проще простого! Суть его заключалась в том, что необходимо было запустить установку ws от рута(возможно и нет, но я решил не рисковать) и дождаться окончания сборки нативных составляющих ws.
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
ACCNCC
@ACCNCC
Делаю игры!
Осторожно npm install ws протекает!
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы