简体   繁体   English

带有 Apache Cordova 的 Node.js 和 Socket.io 应用程序无法在 android 中运行

[英]Node.js and Socket.io app with Apache Cordova not working in android

I'm trying to follow this simple tutorial socket-io-with-apache-cordova for android with apache cordova.我正在尝试按照这个简单的教程socket-io-with-apache-cordova for android with apache cordova。 So far I did everything as in the tutorial and I use my own android phone with android version 8.0 to test the app.到目前为止,我按照教程中的所有内容进行了操作,并使用自己的 android 手机android version 8.0来测试该应用程序。 When I type the cordova run command app get launched and shows the screen with "DEVICE IS READY" and nothing happens.当我输入cordova run命令应用程序被启动并shows the screen with "DEVICE IS READY" ,没有任何反应。 I get this Received Event: deviceready message and following errors in the chrome remote debugging console and nothing happens it doesn't pop up an alert like it supposed to.我在 chrome 远程调试控制台中收到此Received Event: deviceready消息和以下错误,但没有任何反应,它不会像预期的那样弹出警报。

localhost:3000/socket.io/?EIO=2&transport=polling&t=1549179448883-0:1 Failed to load resource: net::ERR_CONNECTION_REFUSED localhost:3000/socket.io/?EIO=2&transport=polling&t=1549179448883-0:1 加载资源失败:net::ERR_CONNECTION_REFUSED

GET http://localhost:3000/socket.io/?EIO=2&transport=polling&t=1549179463998-5 0 () GET http://localhost:3000/socket.io/?EIO=2&transport=polling&t=1549179463998-5 0 ()

This is the index.html这是 index.html

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' http://* 'unsafe-inline'; script-src 'self' http://* 'unsafe-inline' 'unsafe-eval'" />
    <meta name="format-detection" content="telephone=no">
    <meta name="msapplication-tap-highlight" content="no">
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <title>Hello World</title>
</head>

<body>
    <div class="app">
        <h1>Apache Cordova</h1>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p>
        </div>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="http://cdn.socket.io/socket.io-1.0.3.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();

        document.addEventListener('deviceready', function() {
            var socket = io.connect('http://localhost:3000');
            socket.on('connect', function() {
                socket.on('text', function(text) {
                    alert(text);
                });
            });
        });
    </script>
</body>

</html>

And when I change var socket = io.connect('http://localhost:3000');当我改变var socket = io.connect('http://localhost:3000'); like this with my ip address var socket = io.connect('http://172.27.180.225:3000');像这样,我的 IP 地址var socket = io.connect('http://172.27.180.225:3000'); it doesn't give any errors in the console only this Received Event: deviceready message in the console but nothing happens.它不会在控制台中给出任何错误,仅此Received Event: deviceready消息在控制台中,但没有任何反应。

index.js file index.js 文件

var app = {
initialize: function() {
    document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},

onDeviceReady: function() {
    this.receivedEvent('deviceready');
},

receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
}
};

app.initialize();

server.js file server.js 文件

var server = require('http').createServer();
var io = require('socket.io')(server);

io.sockets.on('connection', function(socket) {
console.log('socket connected');

socket.on('disconnect', function() {
    console.log('socket disconnected');
});

socket.emit('text', 'wow. such event. very real time.');
});

server.listen(3000);

index.js and server.js are located at www/js directory And in Package.json I've added these line to start the server index.js and server.js位于www/js目录并且在Package.json我添加了这些行来启动服务器

"main": "server.js",
"scripts": {
    "start": "node server.js"
},

What am i doing wrong ?我究竟做错了什么 ? any help would be appreciated !任何帮助,将不胜感激 !

I think you forgot to start the server: Move server.js to the first folder of your project and start it from the command line with "node server.js" or "npm start", as you defined it in Package.json In the same computer, in another command line, start the project.我想您忘记启动服务器了:将 server.js 移动到项目的第一个文件夹中,并从命令行使用“node server.js”或“npm start”启动它,正如您在 Package.json 中定义的那样同一台电脑,在另一个命令行,启动项目。 I did it with "cordova emulate browser"我用“cordova emulate browser”做到了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM