Source

mongo-dart / lib / objectory / objectory_server.dart

vadimtsushko af4ea1f 














































#import('dart:io');
#import("../mongo.dart");
#import("../bson/bson.dart");

Map<String, String> contentTypes = const {
  "html": "text/html; charset=UTF-8",
  "dart": "application/dart",
  "js": "application/javascript", 
};

List<WebSocketConnection> connections;
void processMessage(message){
  print("in process message $message");
  if (message is String){
    print(message);    
  }
  else{    
    Binary buffer = new Binary.from(message);    
    BsonMap command = new BsonMap(null);
    command.unpackValue(buffer);
    print(command.data);    
  }
}
void main() {
  connections = new List();
  
  WebSocketHandler wsHandler = new WebSocketHandler();
  wsHandler.onOpen = (WebSocketConnection conn) {
    print("Connection opened. Connections total: ${connections.length}");
    connections.add(conn);    
    conn.onClosed = (a, b) => removeConnection(conn);
    conn.onError = (_) => removeConnection(conn);
    conn.send("234234234");    
    conn.onMessage = processMessage;
    
  };

  HttpServer server = new HttpServer();
  server.addRequestHandler((_) => true, wsHandler.onRequest);   

  server.listen("127.0.0.1", 8080);  
}
void removeConnection(WebSocketConnection conn) {
  int index = connections.indexOf(conn);
  if (index > -1) {
    connections.removeRange(index, 1);
  }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.