node.js - Flutter + socket.io 连接

标签 node.js flutter socket.io webrtc

我正在尝试使用 Flutter socket_io_client 连接 NodeJS 套接字。但它没有连接,服务器运行良好。下面是服务器代码,在flutter中我使用了socket_io_client。没有报错,但是还是无法连接。我是socket和nodejs的初学者。帮我看看是什么问题?

myserver.js

const socketio = require('socket.io');
const express = require('express');
const http = require('http');
const app = express();

server = app.listen(3000);
//io server
 //const io = require("socket.io")(server);
//3000 or any other port.
const io = http.createServer(app);
const PORT = 3000 || process.env.PORT;

console.log(`Server running on port ${PORT}`);

var userConnection = [];

io.on('connect', (socket)=> 
{
console.log(`nside connection`);
socket.on('users_info_to_signaling_server', (data) =>
{        
var other_users = userConnection.filter(p=> p.meeting_id == data.meetingid);    
// data saves to userConnection variable
// connection id and socket id are same
userConnection.push({
    connectionId: socket.id,
    user_id: data.current_user_name,
    meeting_id: data.meetingid,
})

})
    
})

flutter 代码

import 'package:flutter/material.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:socket_io_client/socket_io_client.dart' as IO;

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final _localRenderer = new RTCVideoRenderer();
  final _remoteRenderer = new RTCVideoRenderer();
  //final _remoteRenderer2 = new RTCVideoRenderer();

  TextEditingController titleController = TextEditingController();

  IO.Socket socket;

  @override
  void dispose() {
    // TODO: implement dispose
    titleController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    connectToServer();
    super.initState();
  }

  void connectToServer() {
    //initializing with backend server

    socket = IO.io('http://localhost:3000', <String, dynamic>{
      'transports': ['websocket'],
      'autoConnect': false,
    });

    //connection to server
    socket.connect();
    
    socket.onConnect((_) {
     
      if (socket.connected) {
        print('socket connected');
        socket.emit('users_info_to_signaling_server',
            {"current_user_name": "abccheck", "meetingid": "testing"});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Row(
            children: [
              Container(
                height: 210,
                child: Row(
                  children: [
                    Container(
                      margin: EdgeInsets.all(8.0),
                      padding: EdgeInsets.all(8.0),
                      height: 200,
                      width: 350,
                      decoration: BoxDecoration(color: Colors.black),
                      key: Key('local'),
                      child: RTCVideoView(_localRenderer),
                    ),
                    Container(
                      margin: EdgeInsets.all(8.0),
                      padding: EdgeInsets.all(8.0),
                      height: 200,
                      width: 350,
                      decoration: BoxDecoration(color: Colors.black),
                      key: Key('remote'),
                      child: RTCVideoView(_localRenderer),
                    ),
                    Container(
                      margin: EdgeInsets.all(8.0),
                      padding: EdgeInsets.all(8.0),
                      height: 200,
                      width: 350,
                      decoration: BoxDecoration(color: Colors.black),
                      key: Key('remote2'),
                      child: RTCVideoView(_localRenderer),
                    ),
                  ],
                ),
              )
            ],
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextField(
              controller: titleController,
              decoration: InputDecoration(
                hintText: 'Name or MeetingID',
                alignLabelWithHint: true,
              ),
            ),
          ),
          SizedBox(
            height: 8.0,
          ),
          RaisedButton(
            onPressed: () {},
            child: Text('Host'),
          ),
          Padding(
            padding: EdgeInsets.all(8.0),
          ),
          RaisedButton(
            onPressed: () {},
            child: Text('Join'),
          ),
        ],
      ),
    );
  }
}


最佳答案

问题是您需要检查插件的文档,如下所示,存在版本兼容性

Version conflicts

关于node.js - Flutter + socket.io 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67545062/

相关文章:

javascript - Node.js异步循环在下一个循环之前从函数回调

node.js - 如何在 Node.js 应用程序中使用私有(private)安全连接?

reactjs - Flutter (Dart) 中的 RSA 加密

在属性上使用 delete 时的 Javascript 对象内存管理

node.js - 与 'ws://.../socket.io/' 的 WebSocket 连接失败:WebSocket 握手期间出错:net::ERR_CONNECTION_RESET

node.js - 为什么 socket-io 的 get 无法从持久 session 中获取数据?

node.js - 在通用/同构应用程序中组织 package.json 依赖项

node.js - Node.js 的有状态进程间通信

flutter - Navigation.of(context) 不去另一条路线

ios - Flutter - iOS 模拟器不加载 Assets