Flutter套接字io无法连接到节点js套接字io服务器端口3000
节点 JS 服务器
const express = require("express");
const mongoose = require("mongoose");
const app = express();
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
io.on("connection", (socket) => {
console.log("connected");
/* console.log(socket) */
socket.on("msg", (msg) => {
console.log(msg);
});
Cars.watch().on("change", () => {
console.log("Something has changed");
Cars.find()
.sort({ date: -1 })
.then((items) => {
console.log(items);
io.emit("ucer", items);
});
});
});
server.listen(3000, () => {
console.log("listening on port 3000");
});
Flutter 客户端
@override
void initState() {
super.initState();
initializeSocket();
}
void initializeSocket() {
print('initializeSocket');
IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
'transports': ['websocket'],
'autoConnect': false,
});
socket.connect();
}
当运行initializeSocket函数时,当我在终端中等待连接打印时,代码执行没有错误,但是在调试控制台上打印('initializeSocket')。
NODE JS SERVER
const express = require("express");
const mongoose = require("mongoose");
const app = express();
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
io.on("connection", (socket) => {
console.log("connected");
/* console.log(socket) */
socket.on("msg", (msg) => {
console.log(msg);
});
Cars.watch().on("change", () => {
console.log("Something has changed");
Cars.find()
.sort({ date: -1 })
.then((items) => {
console.log(items);
io.emit("ucer", items);
});
});
});
server.listen(3000, () => {
console.log("listening on port 3000");
});
FLUTTER CLIENT SIDE
@override
void initState() {
super.initState();
initializeSocket();
}
void initializeSocket() {
print('initializeSocket');
IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
'transports': ['websocket'],
'autoConnect': false,
});
socket.connect();
}
When the initializeSocket function is run, the code execution without error while i was waiting for connection print in the terminal ,However print('initializeSocket') on the debug console.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
windows
使用此 ipv4 地址代替
localhost:3000
在移动设备中访问Linux
windows
use this ipv4 address to instead of
localhost:3000
access in the mobileLinux