无法连接到twilsoc

时间:2019-05-27 00:32:42

标签: reactjs express twilio

当尝试通过节点连接到twilio并做出反应时,出现了这个奇怪的twilsoc错误。我不知道如何解决此问题。这似乎发生在我的应用程序的服务器端。我已经根据网站上的说明生成了令牌。

index.js:1437 Error: Can't connect to twilsock
        at Upstream.send (upstream.js:245)
        at TwilsockClient.post (client.js:280)
        at network.js:176
        at Retrier.<anonymous> (network.js:114)
        at Retrier.emit (events.js:136)
        at Retrier.attempt (retrier.js:56)
        at retrier.js:111

这是在前端

    componentDidMount() {
    fetch("http://localhost:3001/chat/token", {
      headers: { 'Content-Type': 'application/x-www-form-urlencoded',
     'Access-Control-Allow-Origin': "*",
     'Access-Control-Allow-Headers': "*"
     },
      method: 'POST',
      body: `identity=${encodeURIComponent(this.props.username)}`
    })
      .then(res => res.json())
      .then(data => Chat.create(data.token))
      .then(this.setupChatClient)
      .catch(this.handleError);
  }

这是服务器

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(pino);
app.use(cors());
const sendTokenResponse = (token, res) => {
  res.set('Content-Type', 'application/json');
  res.send(
    JSON.stringify({
      token: token.toJwt()
    })
  );
};

app.get('/api/greeting', (req, res) => {
  const name = req.query.name || 'World';
  res.setHeader('Content-Type', 'application/json');
  res.send(JSON.stringify({ greeting: `Hello ${name}!` }));
});

app.get('/chat/token', (req, res) => {
  const identity = req.query.identity;
  const token = chatToken(identity, config);
  sendTokenResponse(token, res);
});

app.post('/chat/token', (req, res) => {
  console.log('this is firing on the backend')
  const identity = req.query.identity;
  const token = new AccessToken('AC821b3924fcf9577a0eb017c4b21b----', "SK8c95cf6ba0e4a0ec5499d12ae4d----", "o4x7JC9xTEAsZC06SVsnfb2xZU9n----");
  const chatGrant = new ChatGrant({
  serviceSid:"ISdd3f2b55594f45038ac88d84b78e----" ,
});
  token.addGrant(chatGrant);
  token.identity = identity;
  sendTokenResponse(token, res);
});

app.get('/video/token', (req, res) => {
  const identity = req.query.identity;
  const room = req.query.room;
  const token = videoToken(identity, room, config);
  sendTokenResponse(token, res);
});

app.post('/video/token', (req, res) => {
  const identity = req.body.identity;
  const room = req.body.room;
  const token = videoToken(identity, room, config);
  sendTokenResponse(token, res);
});

app.listen(3001, () =>
  console.log('Express server is running on localhost:3001')
);

5 个答案:

答案 0 :(得分:1)

express的最新版本不再使用bodyparser.json,现在已成为express的一部分,请尝试使用:

 express(express.json())

代替

  

express(bodyParser.json())

答案 1 :(得分:1)

这里是Twilio开发人员的传播者。

这实际上看起来像我的代码。这是个好消息,因为它来自我在how to proxy to an Express server with React上的帖子,因此您可以避免CORS问题。如果您使用的是my repo,则应该能够通过运行以下命令来启动服务器和前端应用程序:

npm run dev

那么您不需要从绝对URL fetch来代替,只需使用:

fetch("/chat/token", {
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  method: 'POST',
  body: `identity=${encodeURIComponent(this.props.username)}`
});

然后,webpack开发服务器会将请求代理到Express应用程序。

让我知道是否有帮助。

关于CORS的快速注释

我注意到您正在尝试从您的'Access-Control-Allow-Origin': "*", 'Access-Control-Allow-Headers': "*"请求中传递标头fetch。但是,它们不是请求标头,而是响应标头。如果确实需要CORS标头,则需要Express服务器将其作为响应的一部分返回。

但是正如我所说,我为本文设置代码的方式应该意味着您根本不需要CORS。因此,您现在不必为此担心。

答案 2 :(得分:0)

如果您使用的是Express,最简单的方法是使用cors模块。

首先,使用下面的代码安装它:

npm install cors

接下来,将cors中间件放入快速应用中:

app.use(cors())

如果您想了解更多信息,请阅读cors模块文档https://www.npmjs.com/package/cors

答案 3 :(得分:0)

如果这些解决方案都不起作用(它们对我不起作用),那么可能有一些帮助使错误对我消失:https://stackoverflow.com/a/56671780/404541

答案 4 :(得分:0)

我遇到了这个问题,因为在通过其余API获取令牌时,我拼写了所用的SID。我确保SID正确并且错误消失了。

相关问题