簡體   English   中英

帶有 Apache Cordova 的 Node.js 和 Socket.io 應用程序無法在 android 中運行

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

我正在嘗試按照這個簡單的教程socket-io-with-apache-cordova for android with apache cordova。 到目前為止,我按照教程中的所有內容進行了操作,並使用自己的 android 手機android version 8.0來測試該應用程序。 當我輸入cordova run命令應用程序被啟動並shows the screen with "DEVICE IS READY" ,沒有任何反應。 我在 chrome 遠程調試控制台中收到此Received Event: deviceready消息和以下錯誤,但沒有任何反應,它不會像預期的那樣彈出警報。

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 ()

這是 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>

當我改變var socket = io.connect('http://localhost:3000'); 像這樣,我的 IP 地址var socket = io.connect('http://172.27.180.225:3000'); 它不會在控制台中給出任何錯誤,僅此Received Event: deviceready消息在控制台中,但沒有任何反應。

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 文件

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位於www/js目錄並且在Package.json我添加了這些行來啟動服務器

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

我究竟做錯了什么 ? 任何幫助,將不勝感激 !

我想您忘記啟動服務器了:將 server.js 移動到項目的第一個文件夾中,並從命令行使用“node server.js”或“npm start”啟動它,正如您在 Package.json 中定義的那樣同一台電腦,在另一個命令行,啟動項目。 我用“cordova emulate browser”做到了

暫無
暫無

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

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