-
Notifications
You must be signed in to change notification settings - Fork 9
Walkthrough
Import the client side library for dart force in your clientside file in the web folder.
import 'package:force/force_browser.dart';
First create a client.
ForceClient forceClient = new ForceClient();
forceClient.connect();
Listen on the connection, when it is established.
forceClient.onConnected.listen((e) {
});
Listen on the connection, when it is been broken.
forceClient.onDisconnected.listen((e) {
});
Listen on messages with the request of text.
forceClient.on("text", (e, sender) {
...
});
You can also send messages to the server.
forceClient.send('text', data);
When you are in the need to reply on a message, you can use the 'reply' method of sender.
forceClient.on("text", (e, sender) {
sender.reply("received", ok_data);
});
Import Serverside code for dart force.
import 'package:force/force_serverside.dart';
Instantiate a forceserver.
ForceServer fs = new ForceServer( port: 9223, startPage: 'start.html' );
Other optional properties that are possible on ForceServer:
wsPath: is the websocket path of the server host: is the domain name of your application, by default to localhost port: is the adres port of the application buildPath: is the build path of the application by default this is ../build/web/ startPage: the startpage of the application, the html name that the app needs to use as default root page staticDir: is the public directory where you can put your stylesheets and images
Listen on messages of type text and react upon that.
fs.on('text', (e, sendable) {
var json = e.json;
var line = json['line'];
sendable.send('text', { 'line': line });
});
You can also serve files from the server part.
fs.start().then((_) {
fs.serve("/client.dart").listen((request) {
fs.serveFile("../web/client.dart", request);
});
});
You can listen when a new Socket connection is been created.
fs.onSocket.listen((SocketEvent se) {
// socket event
});