Flutter客户端套接字如何连接到套接字服务器?

时间:2019-09-25 22:19:21

标签: javascript node.js sockets flutter dart

我想通过移动应用程序连接我的套接字服务器,但是它不起作用。
我的服务器代码(Node.js):

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  console.log("biri geldşi")
  socket.on('chat message', function(msg){
    console.log(msg)
    io.emit('chat message', msg);
  });
});

http.listen(port, function(){
  console.log('listening on *:' + port);
});

我的Flutter应用程序代码:

  import 'package:socket_io_client/socket_io_client.dart' as IO;

  main() {
  IO.Socket socket = IO.io('http://192.168.1.102:3000');
  socket.on('connection', (_) {
    print('connect');
    socket.emit('chat message', 'test');
  });
  socket.on('event', (data) => print(data));
  socket.on('disconnect', (_) => print('disconnect'));
  socket.on('fromServer', (_) => print(_));
}

0 个答案:

没有答案