chat

81 views
Skip to first unread message

Илья Савинков

unread,
Apr 16, 2014, 3:57:52 PM4/16/14
to mi...@dartlang.org
server runs only one user to another user sent nothing... What am I doing wrong?

Server code
import 'dart:io';

void main() {
  HttpServer.bind('127.0.0.1', 4040).then((server) {
    server.listen((HttpRequest request) {
      if(request.uri.path == '/chat') {
        WebSocketTransformer.upgrade(request).then((WebSocket socket) {
          socket.listen((msg) {
            socket.add(msg);
          });          
        });
      }
    });
  });
}

Client code
import 'dart:html';

void main() {
   WebSocket ws = new WebSocket('ws://127.0.0.1:4040/chat');
   
   InputElement sendBtn = querySelector('.sendBtn');
   InputElement sendMsg = querySelector('.sendMsg');
   UListElement message = querySelector('.message');
   
   ws.onOpen.listen((e) {    
      sendBtn.onClick.listen((_) {
        ws.send(sendMsg.value);
      });                
   });
   
   ws.onMessage.listen((MessageEvent e) {
     LIElement li = new LIElement();
     li.text = e.data;
     message.children.add(li);
   });   
}


Danny Kirchmeier

unread,
Apr 17, 2014, 1:00:35 PM4/17/14
to mi...@dartlang.org
It would appear that you need to send the messages to other sockets. 
Right now you are sending the message only to the socket who sent the message.

Илья Савинков

unread,
Apr 17, 2014, 1:13:48 PM4/17/14
to mi...@dartlang.org
And how to do that is sent to all users?)

четверг, 17 апреля 2014 г., 23:00:35 UTC+6 пользователь Danny Kirchmeier написал:

Jacob Bang

unread,
Apr 17, 2014, 3:34:51 PM4/17/14
to mi...@dartlang.org
If you want to send the messages you server receive to all connected clients (and not just the client you got the message from) you need to store each connected client in a List and each time you receive a message you need to send the message to all clients in you list:


import 'dart:io';

void main() {
  List<WebSocket> clients = new List();

 
  HttpServer.bind('127.0.0.1', 4040).then((server) {
    server.listen((HttpRequest request) {
      if(request.uri.path == '/chat') {
        WebSocketTransformer.upgrade(request).then((WebSocket socket) {
          clients.add(socket);
         
          socket.listen((msg) {
            clients.forEach((WebSocket client) => client.add(msg));
          });
         
          socket.done.then((_) {
            clients.remove(socket);
          });
        });
      }
    });
  });
}

Another solution is to you use a StreamController.broadcast and subscribe each client to this Stream. When you receive messages you send them to the Stream and all subscribers will get the message:

import 'dart:io';
import 'dart:async';

void main() {
  StreamController controller = new StreamController.broadcast();

 
  HttpServer.bind('127.0.0.1', 4040).then((server) {
    server.listen((HttpRequest request) {
      if(request.uri.path == '/chat') {
        WebSocketTransformer.upgrade(request).then((WebSocket socket) {
          controller.stream.pipe(socket);         
          socket.listen((var msg) {
            controller.sink.add(msg);
          });
        });
      }
    });
  });
}

I like the last solution because it is very easy to send messages to all subscribers from anywhere in you code.


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.



--
Jacob Bang / julemand101
Reply all
Reply to author
Forward
0 new messages