我正在尝试将Socket.io与React Native App一起使用,但是客户端和服务器端之间存在一些问题,我认为代码正确,但是单击按钮时它不会更新状态,连接是完成,我显示日志正常工作,但我没有显示任何错误!
那是我的代码:
server / app.js
<li class="blocks-gallery-item">
<figure>
<img data-src="http://localhost:3000/wp-content/uploads/2018/11/detektivhut.gif" alt="" data-id="1037" data-link="http://localhost:3000/detektivhut/" class="wp-image-1037"/>
</figure>
</li>
<li class="blocks-gallery-item">
<figure>
<img data-src="http://localhost:3000/wp-content/uploads/2018/11/DSC04828.png" alt="" data-id="948" data-link="http://localhost:3000/dsc04828-2/" class="wp-image-948" data-srcset="//localhost:3000/wp-content/uploads/2018/11/DSC04828.png 1067w, //localhost:3000/wp-content/uploads/2018/11/DSC04828-200x300.png 200w, //localhost:3000/wp-content/uploads/2018/11/DSC04828-768x1152.png 768w, //localhost:3000/wp-content/uploads/2018/11/DSC04828-683x1024.png 683w, //localhost:3000/wp-content/uploads/2018/11/DSC04828-1000x1500.png 1000w" sizes="(max-width: 1067px) 100vw, 1067px" />
</figure>
</li>
<li class="blocks-gallery-item">
<figure>
<img data-src="http://localhost:3000/wp-content/uploads/2018/11/DSC04831.png" alt="" data-id="883" data-link="http://localhost:3000/2018/11/13/single-page-style-1/dsc04831-2/" class="wp-image-883" data-srcset="//localhost:3000/wp-content/uploads/2018/11/DSC04831.png 1067w, //localhost:3000/wp-content/uploads/2018/11/DSC04831-200x300.png 200w, //localhost:3000/wp-content/uploads/2018/11/DSC04831-768x1152.png 768w, //localhost:3000/wp-content/uploads/2018/11/DSC04831-683x1024.png 683w, //localhost:3000/wp-content/uploads/2018/11/DSC04831-1000x1500.png 1000w" sizes="(max-width: 1067px) 100vw, 1067px" />
</figure>
</li>
server / index.html
var app = require("express")();
var server = require("http").Server(app);
var io = require("socket.io")(server);
server.listen(8080);
app.get("/", function(req, res) {
res.sendFile(__dirname + "/index.html");
});
io.on("connection", function(socket) {
console.log(socket.id);
socket.on("update", () => {
console.log("update con");
socket.emit("update");
});
});
App.js
<h1>Welcome Socket.io !!</h1>
<button>Update</button>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
var btn = document.querySelector("button");
btn.onclick = function() {
console.log("update func");
socket.emit("update");
};
</script>
答案 0 :(得分:0)
我认为问题出在您的套接字服务器的地址上。在App.js
中,您编写了localhost,但如果它在移动设备,模拟器或设备中,则您的地址不能为localhost
。适用于Android模拟器或PC和设备共享的WiFi上的PC地址可能为10.0.2.2。