What are my options for converting a socketio nodejs application to dart? Is there support for nodejs servers using dart somehow (ideally with all the fancy debugging capabilities of the dart editor)? Does socketio have a dart based library?
What are my options for converting a socketio nodejs application to dart? Is there support for nodejs servers using dart somehow (ideally with all the fancy debugging capabilities of the dart editor)? Does socketio have a dart based library?
Share Improve this question asked Jul 15, 2012 at 19:11 DestedDested 6,43312 gold badges53 silver badges73 bronze badges 2- Try the port of SocketIO to Dart: github.com/petrh/duct – Seth Ladd Commented Jul 18, 2012 at 16:09
- The correct URL to the Dart port of SocketIO is in this answer stackoverflow.com/a/15647977/301740 – Draško Kokić Commented Dec 8, 2013 at 14:51
1 Answer
Reset to default 19Dart has a server side VM, just like V8 has a server side VM in the form of node.js.
Take a look at Adam Smith's webserver chat sample, which uses websockets on the server side to communicate with websockets on the client side, with both parts being written in Dart.
The key parts for the server side look like:
import "dart:io";
main() {
HttpServer server = new HttpServer();
WebSocketHandler wsHandler = new WebSocketHandler();
server.addRequestHandler((req) => req.path == "/ws", wsHandler.onRequest);
wsHandler.onOpen = (WebSocketConnection conn) {
conn.onMessage = (message) {
print(message);
conn.send("hello, this is the server");
};
};
server.listen("127.0.0.1",8080);
}
Then on the client, something like
import "dart:html";
main() {
var ws = new WebSocket("ws://127.0.0.1:8080/ws");
ws.on.open.add( (a) {
ws.send("hello, this is the client");
});
ws.on.message.add( (messsage) {
print(message);
});
}