Flutter套接字io无法连接到节点js套接字io服务器端口3000

发布于 2025-01-10 13:02:40 字数 1472 浏览 0 评论 0原文

节点 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')。

heroku 节点 js

颤振套接字

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.

heroku node js

flutter sockets

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉态萌生 2025-01-17 13:02:40

windows

使用此 ipv4 地址代替 localhost:3000 在移动设备中访问
输入图片此处描述

Linux

在此处输入图像描述

windows

use this ipv4 address to instead of localhost:3000 access in the mobile
enter image description here

Linux

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文