gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:08:11 25 4
gpt4 key购买 nike

我正在尝试使用apache cordova针对Android编写此简单教程socket-io-with-apache-cordova。到目前为止,我已经完成了本教程中的所有操作,并使用自己的Android手机和android version 8.0测试了该应用程序。当我键入cordova run命令应用时,启动并启动shows the screen with "DEVICE IS READY",什么也没有发生。我收到此Received Event: deviceready消息,并在chrome远程调试控制台中出现以下错误,并且没有任何 react ,它没有像预期的那样弹出警报。

localhost:3000/socket.io/?EIO=2&transport=polling&t=1549179448883-0:1 Failed to load resource: 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>

而且,当我用自己的IP地址 var socket = io.connect('http://localhost:3000');更改 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移至项目的第一个文件夹,然后使用Package.json中定义的“node server.js”或“npm start”从命令行启动它。
在同一台计算机上的另一个命令行中,启动项目。我是用“cordova模拟浏览器”完成的

关于android - 使用Apache Cordova的Node.js和Socket.io应用程序无法在android中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54500992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com