簡體   English   中英

angular 8和sailsJs在socket io請求中出錯

[英]error in socket io request by angular 8 and sailsJs

我是 socket.io 和 Angular 的新手,我向sailsJS(后端)提出了一個套接字請求,但在請求后我收到以下錯誤:

Sails 回應:錯誤:在 Express 4/Sails v1 中不再支持res.json()的 2-ary 用法。 請改用res.status(statusCode).json(body)

角度:

import { Injectable } from '@angular/core';
import * as io from 'socket.io-client';
import * as sailsIOClient from 'sails.io.js';
import {Observable} from 'rxjs';
@Injectable({
  providedIn: 'root'
})
export class SocketIoService {
  private socket = sailsIOClient(io);
  constructor() {
    this.socket.sails.url = 'http://localhost:1337';
  }

  sendRequest() {
    console.log('call sokcet')
    this.socket.socket.get('/socket', (resData) => {
      console.log('Sails responded with: ', resData);
    });
  }
}

在sails js中,我有以下配置和代碼:在routes.js中:

'get /socket': 'TaskController.Index',

在 TaskController.js 中:

module.exports = {
  Index: function (req, res) {
    console.log('in sails socket', req.isSocket);
    if( !req.isSocket) {
      return res.badRequest();
    }
    sails.sockets.join(req.socket, 'task');
    sails.sockets.broadcast('task', 'list');
    // sails.sockets.blast('task', 'entry');
    return res.ok();
  }
}

在 socket.js 中:

transports: ['polling', 'websocket'],

我通過更改令牌授權函數中的“返回代碼”解決了上述問題:已解決代碼:

return res.status(401).json({err: 'No Authorization header was found'});

舊代碼(拋出錯誤):

 return res.json(401, {err: 'No Authorization header was found'});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM