serverもclientもDartでリアルタイムWeb

memo

Dartのリアルタイムサーバー。とりあえずメモ

Nitrous.ioでの作業ログ

git clone https://github.com/jorishermans/dart-force-chat-example 

#server とclient両方で実行する
pub get

#client側で実行
pub build

#server.dartのcreate force serverの部分を以下に書き換え
  // Create a force server
  ForceServer fs = new ForceServer(host:'0.0.0.0',
                                   port: port, 
                                   clientFiles: '../client/build/web/',
                                   clientServe: serveClient);
#実行
dart server.dart

軽く動かしてみた

http://dart-113551.apne1.nitrousbox.com:8080/#

とりえずコード読んだりして見る予定。

revelのJobs #golangjp

jobsを使ってみる

http://robfig.github.io/revel/manual/jobs.html

jobはModulesの一種。 revelフレームワークに機能を追加したりするよ。

設定

app/conf/app.confに以下を追加
module.jobs = github.com/robfig/revel/modules/jobs

job監視をするページを見たい場合はapp/conf/routesに以下を記述
module:jobs

使い方

ひとまずinit.goに定期実行するfuncを記述。
以下のコードは一分毎に「1minute」と出力するサンプル。

func init() {

    revel.OnAppStart(func() {
        fmt.Println("onAppStart")
        jobs.Schedule("0 * * * * *", jobs.Func(func() { fmt.Println("1minute") }))
    })
}
例1:functionを指定する。

revel.OnAppStart(func() {})でサーバ起動前にfunc実行
jobs.Schedule("0 * * * * *", jobs.Func(func() { fmt.Println("1minute") }))
時間の設定はcronと同様。

例2:Run() {}を実装した構造体を指定する。

src/github.com/robfig/revel/samples/booking/app/jobs/count.go
を見るとわかりやすい。init*1にOnAppStartを実行してstructを登録している。
登録するstructはrun()を実装している。

*1:main.goにてimportされるのでinitも動く仕組み。

revelのexample bookingを動かす。#golangjp

http://robfig.github.io/revel/samples/booking.html

Mac OSXでhomebrewがインストールしてある前提

  • brew install pkgconfig sqlite3
  • revel run github.com/robfig/revel/samples/booking

おこ

Go Compilation Error
The Go code ../github.com/robfig/revel/samples/booking/app/tmp/main.go does not compile: cannot find package >"github.com/mattn/go-sqlite3" in any of:

おこ

go get github.com/mattn/go-sqlite3

pkg-config --cflags sqlite3

Package sqlite3 was not found in the pkg-config search path.
Perhaps you should add the directory containing `sqlite3.pc'
to the PKG_CONFIG_PATH environment variable
No package 'sqlite3' found
exit status 1

なので
.bash_profileに

export PKG_CONFIG_PATH=/usr/local/Cellar/sqlite/3.7.17/lib/pkgconfig

これもおこられたので入れる

go get github.com/robfig/cron

できた。

f:id:mogetta:20130820153416p:plain

revelのメモ

revel

http://robfig.github.io/revel/

quick start(go言語インストール, GOPATH設定済み)
  • go get github.com/robfig/revel/revel
  • go install revel
  • revel new myapp
  • revel run myapp #myappがアプリ名

で、$GOPATH/src/myappにテンプレートが展開。

構成(例

myapp
├── app
│   ├── controllers #コントローラ
│   ├── init.go
│   ├── routes #自動生成
│   ├── tmp #自動生成
│   └── views #テンプレートなど
│   ├── App #コントローラーの名前に対応したディレクトリにfunc名.htmlを格納
│   ├── errors
├── conf #設定ファイル
├── messages
├── public #静的ファイル格納
│   ├── css
│   ├── images
│   └── js
└── tests #テストファイル

メモ
  • エントリーポイントはmyapp/app/tmp/main.go これは「GENERATED CODE - DO NOT EDIT」なので変更しないこと(変更しても書き換えられる)
  • URLとController:myapp/app/view/Controller名/func名.htmlが呼び出される
  • revel runした状態でcontroller内に追記すると即座に反映される。templateも反映
  • myapp/conf/app.confで設定の変更: 例:ポート変更ならhttp.port=9000
  • myapp/conf/routeでルーティング

Dart cheat sheat

Dart cheat sheat

自分用の備忘録

import

import 'dart:html';
import 'xxx:xxx'    as xxx;

entry point

main() {
  query('#status').text = 'Hi, Dart';
}

print log debug

num aNumber
print('The number is $aNumber.');
$variableName (or ${expression})

List

var list = [1,2,3];
assert(list.length == 3);
assert(list[1] == 2);

Map

var gifts = {                         // A map literal
// Keys       Values
  'first'  : 'partridge',
  'second' : 'turtledoves',
  'fifth'  : 'golden rings'
};

var map = new Map();                  // Use a map constructor.
map[1] = 'partridge';                 // Key is 1; value is 'partridge'.
map[2] = 'turtledoves';               // Key is 2; value is 'turtledoves'.
map[5] = 'golden rings';              // Key is 5; value is 'golden rings'.

A Quick Look at the Dart Language

import 'dart:math';

class Point {
  num x, y;
  Point(this.x, this.y);
  num distanceTo(Point other) {
    var dx = x - other.x;
    var dy = y - other.y;
    return sqrt(dx * dx + dy * dy);
  }
}

main() {
  var p = new Point(2, 3);
  var q = new Point(3, 4);
  print('distance from p to q = ${p.distanceTo(q)}');
}

dartlang 俺専用まとめ 随時更新

Dart言語俺専用まとめ。(2013/05/10)

Dart(Dart言語、Dartlang)は、Googleが開発している言語です。

役割的にはjavascriptと似た位置にいる感じですね。 serverとかも立てられるし。

現在はDartium等の専用ブラウザでしか動作しませんが、 dart2jsによってjavascriptに変換させることや jsライブラリでjavascript contextに相互アクセス等ができるので現在の開発環境にも考慮されています。

dartを知る

dartを便利に使う

俺blog