Node.js 颤振+;socket.io连接
我正在尝试使用flattersocket\u io\u客户端连接nodejs套接字。但它无法连接,服务器运行正常。下面是服务器代码,在flatter中我使用了socket\u io\u客户端。没有错误,但仍无法连接。我是socket和nodejs的初学者。帮我找出问题所在 myserver.jsNode.js 颤振+;socket.io连接,node.js,flutter,socket.io,webrtc,Node.js,Flutter,Socket.io,Webrtc,我正在尝试使用flattersocket\u io\u客户端连接nodejs套接字。但它无法连接,服务器运行正常。下面是服务器代码,在flatter中我使用了socket\u io\u客户端。没有错误,但仍无法连接。我是socket和nodejs的初学者。帮我找出问题所在 myserver.js const socketio = require('socket.io'); const express = require('express'); const http = require('http
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,
})
})
})
颤振代码
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'),
),
],
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“包:flatter_webrtc/flatter_webrtc.dart”;
将“package:socket\u io\u client/socket\u io\u client.dart”导入io;
类主页扩展了StatefulWidget{
@凌驾
_HomePageState createState()=>\u HomePageState();
}
类_HomePageState扩展状态{
final _localRenderer=新的RTCVideoRenderer();
final _remoteRenderer=新的RTCVideoRenderer();
//final _remoterender2=新的RTCVideoRenderer();
TextEditingController标题控制器=TextEditingController();
IO.插座;
@凌驾
无效处置(){
//TODO:实现dispose
titleController.dispose();
super.dispose();
}
@凌驾
void initState(){
connectToServer();
super.initState();
}
void connectToServer(){
//使用后端服务器初始化
socket=IO.IO('http://localhost:3000', {
“传输”:[“websocket”],
“自动连接”:错误,
});
//与服务器的连接
socket.connect();
插座未连接((){
if(插座连接){
打印(“插座连接”);
socket.emit('users\u info\u to\u signaling\u server',
{“当前用户名”:“abccheck”,“会议ID”:“testing”});
}
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:专栏(
儿童:[
划船(
儿童:[
容器(
身高:210,
孩子:排(
儿童:[
容器(
边距:所有边集(8.0),
填充:边缘设置。全部(8.0),
身高:200,
宽度:350,
装饰:盒子装饰(颜色:颜色。黑色),
key:key('local'),
子级:RTCVideoView(_localRenderer),
),
容器(
边距:所有边集(8.0),
填充:边缘设置。全部(8.0),
身高:200,
宽度:350,
装饰:盒子装饰(颜色:颜色。黑色),
key:key('remote'),
子级:RTCVideoView(_localRenderer),
),
容器(
边距:所有边集(8.0),
填充:边缘设置。全部(8.0),
身高:200,
宽度:350,
装饰:盒子装饰(颜色:颜色。黑色),
key:key('remote2'),
子级:RTCVideoView(_localRenderer),
),
],
),
)
],
),
填充物(
填充:常数边集全部(8.0),
孩子:TextField(
控制器:标题控制器,
装饰:输入装饰(
hintText:“姓名或会议ID”,
alignLabelWithHint:正确,
),
),
),
大小盒子(
身高:8.0,
),
升起的按钮(
按下:(){},
子项:文本('Host'),
),
填充物(
填充:边缘设置。全部(8.0),
),
升起的按钮(
按下:(){},
子项:文本('Join'),
),
],
),
);
}
}