Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React本地Websocket外部访问_Javascript_React Native_Websocket - Fatal编程技术网

Javascript React本地Websocket外部访问

Javascript React本地Websocket外部访问,javascript,react-native,websocket,Javascript,React Native,Websocket,我试图从一个不包含websocket的组件向我的websocket服务器发送应答。我的Websocket服务器如下所示: componentDidMount() { var ws = new WebSocket('ws:// URL'); ws.onmessage = this.handleMessage.bind(this); ... } let instance = null; class WebsocketController{

我试图从一个不包含websocket的组件向我的websocket服务器发送应答。我的Websocket服务器如下所示:

    componentDidMount() {
    var ws = new WebSocket('ws:// URL');
    ws.onmessage = this.handleMessage.bind(this);
    ...
    }
    let instance = null;

    class WebsocketController{
        constructor() {
            if(!instance){
               instance = this;
            }
            this.ws = new WebSocket('ws://URL');
            return instance;
        }
    }

    export default WebsocketController
    let controller = new WebsocketController();
    var ws = controller.ws;
如何将var ws传递给另一个类或组件。或者可以使websocket全局可访问吗? 非常感谢您的帮助

websocket连接

将此代码保存在某个文件中,并用.js extension命名。例:websocket.js

var WebSocketServer = require("ws").Server;

var wss = new WebSocketServer({port:8100});

wss.broadcast = function broadcast(msg) {
    console.log(msg);
    wss.clients.forEach(function each(client) {
        client.send(msg);
    });
};

wss.on('connection', function connection(ws) {
    // Store the remote systems IP address as "remoteIp".
    var remoteIp = ws.upgradeReq.connection.remoteAddress;
    // Print a log with the IP of the client that connected.
    console.log('Connection received: ', remoteIp);

    ws.send('You successfully connected to the websocket.');

    ws.on('message',wss.broadcast);
});
在应用程序/网站端。创建.js文件。例:client.js

var SERVER_URL = 'ws://127.0.0.1:8100';
var ws;

function connect() {
  //alert('connect');
    ws = new WebSocket(SERVER_URL, []);
    // Set the function to be called when a message is received.
    ws.onmessage = handleMessageReceived;
    // Set the function to be called when we have connected to the server.
    ws.onopen = handleConnected;
    // Set the function to be called when an error occurs.
    ws.onerror = handleError;

}

function handleMessageReceived(data) {
    // Simply call logMessage(), passing the received data.
    logMessage(data.data);
}

function handleConnected(data) {
    // Create a log message which explains what has happened and includes
    // the url we have connected too.
    var logMsg = 'Connected to server: ' + data.target.url;
    // Add the message to the log.
    logMessage(logMsg)

    ws.send("hi am raj");
}

function handleError(err) {
    // Print the error to the console so we can debug it.
    console.log("Error: ", err);
}

function logMessage(msg) {
    // $apply() ensures that the elements on the page are updated
    // with the new message.
    $scope.$apply(function() {
        //Append out new message to our message log. The \n means new line.
        $scope.messageLog = $scope.messageLog + msg + "\n";
    });

}

如果您遇到与此代码有关的任何问题,请告诉我

我在stackoverflow中找到了一个解决方案,该问题提供了帮助:

访问:

我创建了一个新类WebsocketController,如下所示:

    componentDidMount() {
    var ws = new WebSocket('ws:// URL');
    ws.onmessage = this.handleMessage.bind(this);
    ...
    }
    let instance = null;

    class WebsocketController{
        constructor() {
            if(!instance){
               instance = this;
            }
            this.ws = new WebSocket('ws://URL');
            return instance;
        }
    }

    export default WebsocketController
    let controller = new WebsocketController();
    var ws = controller.ws;
在我的另一节课上,我需要我的websocket,我只是这样称呼它:

    componentDidMount() {
    var ws = new WebSocket('ws:// URL');
    ws.onmessage = this.handleMessage.bind(this);
    ...
    }
    let instance = null;

    class WebsocketController{
        constructor() {
            if(!instance){
               instance = this;
            }
            this.ws = new WebSocket('ws://URL');
            return instance;
        }
    }

    export default WebsocketController
    let controller = new WebsocketController();
    var ws = controller.ws;

我认为首先你应该连接到websocket,一旦连接,只有你才能发送。在本地系统中进行测试。例如,给定路径为ws://127.0.0.1:8100您用于导航的内容是相关的我使用的是react navigation,实际上它没有识别出这一点:var WebSocketServer=requirews.Server;安装此:npm安装wsI找到了另一个解决方案,请参阅下面我发布的答案OK您可以使用任何解决方案。ya。不客气。我还在websocket上工作。你可以问你是否在代码中的任何地方卡住了你的代码让我看得更远。我找到了这个链接,我用它作为灵感。这就是所谓的单身