小编Wor*_*ker的帖子

无法构造'WebSocket':URL'[object Object]'无效

我试图与websocket建立连接并在React Router中获取数据。连接未发生,并且Uncaught SyntaxError: Failed to construct 'WebSocket': The URL '[object Object]' is invalid.在浏览器控制台中出现错误。使用npm start从终端到启动应用程序。这是一个简单的反应路由器应用程序。以下是我认为发生问题的代码。

import React from 'react'

export default React.createClass({
  getInitialState: function() {
     return { value : [],
                  client : '' };
  },

   componentWillMount: function() {
      client = new WebSocket("ws://localhost:8000/","echo-protocol");
      client.onerror = function() {
           console.log('Connection Error');
       };

        client.onopen = function() {
          function sendNumber() {
               if (client.readyState === client.OPEN) {
                   var number = Math.round(Math.random() * 0xFFFFFF);
                   client.send(number.toString());
                   setTimeout(sendNumber, 1000);
               }
           }
           sendNumber();
       };

    client.onmessage = function(e) …
Run Code Online (Sandbox Code Playgroud)

npm browserify reactjs webpack react-router

5
推荐指数
1
解决办法
7635
查看次数

标签 统计

browserify ×1

npm ×1

react-router ×1

reactjs ×1

webpack ×1