Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket not working when I do a pub build #2

Closed
smrtsmrf opened this issue Jul 25, 2017 · 3 comments
Closed

Socket not working when I do a pub build #2

smrtsmrf opened this issue Jul 25, 2017 · 3 comments
Labels

Comments

@smrtsmrf
Copy link

smrtsmrf commented Jul 25, 2017

I've got the socket working when I have my client files running on pub serve through Webstorm. (Although the connection is sometimes finnicky.)

But when I do a pub build so that I can serve up the files from my server, the socket doesn't seem to work at all.

Here's my (relevant) dart client code:

void ngOnInit() {
    name = _routeParams.get('name');
    socket = IO.io('http://localhost:3000');
    socket.connect();
    socket.on('connect', (_) {
      print('Socket connected (client)');
      // need to send something stupid initially so that it will keep listening
      socket.emit('client', 1);

      socket.on('server', (_) => print('handshake'));
      
      countDownTimer = new Timer.periodic(const Duration(seconds: 1), (Timer countDownTimer) {
        countDown--;
        timerMsg = countDown > 0 ? 'BEGIN IN ${countDown}' : 'GO!';

        if (countDown == 0) {
          socket.emit('start game', 2);
          countDownTimer.cancel();
          gameInProgress = true;
          
          gameTimer = new Timer.periodic(const Duration(seconds: 1), (Timer gameTimer) {
            gameTime--;
            gameTimerMsg = gameTime > 0 ? gameTime.toString() : 'END GAME';

            if (gameTime == 0) {
              print('game over');

              
              print('updateLeaderboard');
              Leader player = new Leader(name, 25);
              _leaderboardService.leaders.add(player);
              _leaderboardService.leaders.sort((a, b) => b.score.compareTo(a.score));
              print('about to end game');
              socket.emit('end game', 'i am a string');
              print('should have ended game');
              gameTimer.cancel();
              this._router.navigate(['Leaderboard', player.toMap()]);
            }
          });
        }
      });
    });

    socket.on('new color', (data) {
      print('new color: $data');
      HTML.Animation animation;
      HTML.Element element;
      switch (data) {
        case 1:
          element = green.nativeElement; break;
        case 2:
          element = red.nativeElement; break;
        case 3:
          element = yellow.nativeElement; break;
        case 4:
          element = blue.nativeElement; break;
      }
      animation = element.animate([{"opacity": 100}, {"opacity": 0}], 150);
      animation.play();
    });

    socket.on('point', (_) => score++);
    
    socket.on('ended game', (_) {
      socket.clearListeners();
      socket.disconnect();
    });

    socket.on('disconnected', (_) => print('socket disconnected (server)'));
  
}

And here's the errors I'm getting in Chrome:
screen shot 2017-07-25 at 8 27 15 am

jumperchen added a commit that referenced this issue Jul 26, 2017
1. fix logger issue.
2. fix the issue of the changing transport from polling to websocket
3. make a test case
@jumperchen
Copy link
Member

I fixed some issues which may relate to this issue, you can give it a try. (remember to run pub upgrade first), and I also make a test case with a timer to demonstrate the problem you encountered, but it works well for me on both dartium or browser (compiling to js) environment.

@NickWu007
Copy link

Hi,

I have been having the same problem, but slightly different.

On pub serve things are working fine, but on pub build, locally I would get a few 400 errors but eventually succeed to connect. Below is a screenshot:

screen shot 2018-02-01 at 16 24 04

I am also deploying my app on digital ocean VPS and over there I would get a huge number of 400 errors(with same response body), and finally connect after 1 or 2 minutes. Below is a screenshot of network activities on VPS.

screen shot 2018-01-30 at 20 53 21
screen shot 2018-01-30 at 20 56 02

My server is written in node:

var app = express()
var server = http.createServer(app);
server.listen(port);
var io = require('socket.io').listen(server);

I am using namespaces also, for each client:

var socketPath = '/' + req.session.user.username
io.of(socketPath).on('connection', function (socket) {
    ...
}

Client:

String namespace = 'http://localhost:5000' + socketPath;
print('connecting to $namespace');
socket = IO.io(namespace);
socket.on('connect', (_) {
        print('connected to $namespace');
        ....
}

As far as I can see there's very little custom config used. Could you please take a look as this? I can discuss more in detail. This lib is critical for my project, so please help.

Thanks,
Nick

@jumperchen
Copy link
Member

This issue is fixed in https://github.com/rikulo/socket.io-dart, so you should upgrade them by running pub upgrade.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants