今日はwebsocket

なんだか、話が二転三転して結局何も作れないエンジニアになってしまいそうだが、今日はwebsocket導入についてのメモを。

websocket

HTML5で導入された通信プロトコル。サーバプッシュ型のアプリケーションを開発できる。今回、チャットを使ったwebアプリを構築するために、この技術を使うことにした。

ポート

今回、既に80番ではapache上でwebメールやらなんやらが動いている状態なので、新しいアプリケーション用にいくつかportを開放する必要がある。

8080番

webrickとかのAPサーバ用。

3000番

websocketサーバ用。

843番

socket-policy用。

クライアント

対応ブラウザは現状chromefirefox4系のみ。(safariも?)その他のブラウザでは、web-socket.js+flashを使う方法がある。この場合、サーバ上にsocket-policyファイルを配置しなければならない。また、個別にアプリケーション実装する場合は、上記サーバのF/Wを利用することもできるぽい。(やってない)

websocketプロトコルの仕様

draft75と76でhandshakeの仕様が大きく変わった。そのため、サーバとクライアントで、実装された仕様を合わせなければならない。

対応状況(試した範囲)

draft75

クライアント:chrome5系 サーバ:rev-websocket

draft76

クライアント:chrome6系(β)、web-socket.js サーバ:mojo、rev-websocket

⇒新しい仕様であることも踏まえると、draft76対応にしたほうがよい。chrome5系が使えないのがネックだが・・・。

socket-policyファイルについて

現状chrome6系を導入している人はあまり多くないと思うので、web-socket.jsに対応させたほうがいいと思われる。そのためにsocket-policyを導入しなければならないが、基本的には
有効なWikiNameではありません - namespace gimite
に書いてある通りにすればよい。ubuntuを使っている場合、inetdにopenbsd-inetdとxinetdの選択肢があるが、上記サイトの手順に従う場合はopenbsd-inetdを導入する。

F/W選定

イコールサーバ選定ということである。

mojo

perl。get/postとwebsocketを一つのポートで使えるみたい。mojolicious::liteを使った場合、静的ファイルの置き場所が分かりにくいが、スクリプトの置いてあるディレクトリにpublicを作ると、そこがdocrootになる。

rev-websocket

rubywebrickとwebsocketサーバを別々に立てるので、get/post用に別ポートを開放する必要がある?それだったらapache+rev-websocketという選択肢もありそうだが、ポータビリティが下がるか。そもそものポータビリティはmojoに劣る気がする。利点がdraft75にも対応できるくらいしかないか・・・?

node.websocket.js

サーバサイドもjs。js好きなので悪くない選択肢。apache、またはnode.jsと組み合わせる。こちらもポータビリティはあまり良くないが、mojo+node.websocket.jsっていうのも面白い?node.jsの学習も必要だなあ。が、全体として未成熟感は否めない。

その他

c++しか分からないのですが・・・

⇒現状ではやはりmojoが有力。

そろそろ

webサーバくらい書けるようになっておかねばなりますまい。

HaskellでWebアプリを作ってみようと思ったので、その作業メモというか。

まずは、Webフレームワークの選定を行った。候補としては、

が有力。WASHは、ふつうのHaskellプログラミング ふつうのプログラマのための関数型言語入門でも一瞬触れられていたような記憶がある。が、なんとなくhttp://happstack.com/index.htmlが気に入ったので、そっちを導入してみることにする。HPの説明にしたがって、tutorialを導入してみる。

$ darcs get http://patch-tag.com/r/wchogg/happstack-tutorial/pullrepo happstack-tutorial
$ cabal install

普通にエラーになりました(エラーメッセージは残ってません)。cabalを入れないとだめらしいので、頑張って入れてみた。

$ wget http://hackage.haskell.org/packages/archive/cabal-install/0.8.2/cabal-install-0.8.2.tar.gz
$ tar -zxf cabal-install-0.8.2.tar.gz
$ cd cabal-install-0.8.2
$ sh ./bootstrap.sh

これもエラーメッセージが残っていないのですが、timeが無い的なことを言われて終了。ググってみたところ、下のような感じで入っているのかどうかは確認できるらしい。

$ ghc-pkg list time

どうも、そもそも入っていないらしい。aptで探してみたらあるっぽいので、apt-getでインストール。

$ sudo apt-get install libghc6-time-doc libghc6-time-prof libghc6-time-dev
$ ghc-pkg list time

ちゃんと入ったぽいので、再びチャレンジ。

$ sh ./bootstrap.sh

インストールできたぽい?PATHの通った場所に出来たcabalを移動。

$ sudo cp .cabal/bin/cabal /usr/bin/
$ cabal --help
This program is the command line interface to the Haskell Cabal infrastructure.
See http://www.haskell.org/cabal/ for more information.

Usage: cabal COMMAND [FLAGS]
   or: cabal [GLOBAL FLAGS]

Global flags:
 -h --help            Show this help text
 -V --version         Print version information
    --numeric-version Print just the version number

Commands:
  install      Installs a list of packages.
  update       Updates list of known packages
  list         List packages matching a search string.
  info         Display detailed information about a particular package.
  upgrade      Upgrades installed packages to the latest available version
  fetch        Downloads packages for later installation.
  unpack       Unpacks packages for user inspection.
  check        Check the package for common mistakes
  sdist        Generate a source distribution file (.tar.gz).
  upload       Uploads source packages to Hackage
  report       Upload build reports to a remote server.
  init         Interactively create a .cabal file.
  configure    Prepare to build the package.
  build        Make this package ready for installation.
  copy         Copy the files into the install locations.
  haddock      Generate Haddock HTML documentation.
  clean        Clean up after a build.
  hscolour     Generate HsColour colourised code, in HTML format.
  register     Register this package with the compiler.
  test         Run the test suite, if any (configure with UserHooks).
  help         Help about commands

For more information about a command use:
  cabal COMMAND --help

To install Cabal packages from hackage use:
  cabal install foo [--dry-run]

Occasionally you need to update the list of available packages:
  cabal update

You can edit the cabal configuration file to set defaults:
  /home/***/.cabal/config

うまくいった気がするので、再度tutorialを入れてみる。

$ cd tmp/darcs/happstack-tutorial/
$ cabal install
Resolving dependencies...
[1 of 1] Compiling Main             ( /tmp/haskell-src-exts-1.9.03187/haskell-src-exts-1.9.0/Setup.hs, /tmp/haskell-src-exts-1.9.03187/haskell-src-exts-1.9.0/dist/setup/Main.o )
Linking /tmp/haskell-src-exts-1.9.03187/haskell-src-exts-1.9.0/dist/setup/setup ...
Configuring haskell-src-exts-1.9.0...
setup: happy version >=1.17 is required but it could not be found.
Configuring strict-concurrency-0.2.3...
Preprocessing library strict-concurrency-0.2.3...
Building strict-concurrency-0.2.3...
[1 of 2] Compiling Control.Concurrent.MVar.Strict ( Control/Concurrent/MVar/Strict.hs, dist/build/Control/Concurrent/MVar/Strict.o )

Control/Concurrent/MVar/Strict.hs:63:12:
    Not in scope: type constructor or class `NFData'

Control/Concurrent/MVar/Strict.hs:65:26: Not in scope: `rnf'

Control/Concurrent/MVar/Strict.hs:74:15:
    Not in scope: type constructor or class `NFData'

Control/Concurrent/MVar/Strict.hs:83:11:
    Not in scope: type constructor or class `NFData'

Control/Concurrent/MVar/Strict.hs:93:12:
    Not in scope: type constructor or class `NFData'

Control/Concurrent/MVar/Strict.hs:105:12:
    Not in scope: type constructor or class `NFData'

Control/Concurrent/MVar/Strict.hs:120:12:
    Not in scope: type constructor or class `NFData'

Control/Concurrent/MVar/Strict.hs:134:15:
    Not in scope: type constructor or class `NFData'

Control/Concurrent/MVar/Strict.hs:146:14:
    Not in scope: type constructor or class `NFData'
cabal: Error: some packages failed to install:
HJScript-0.5.0 depends on haskell-src-exts-1.9.0 which failed to install.
happs-tutorial-0.9.4 depends on strict-concurrency-0.2.3 which failed to
install.
happstack-0.5.0.2 depends on strict-concurrency-0.2.3 which failed to install.
happstack-data-0.5.0.2 depends on strict-concurrency-0.2.3 which failed to
install.
happstack-helpers-0.51 depends on strict-concurrency-0.2.3 which failed to
install.
happstack-ixset-0.5.0.2 depends on strict-concurrency-0.2.3 which failed to
install.
happstack-server-0.5.0.2 depends on strict-concurrency-0.2.3 which failed to
install.
happstack-state-0.5.0.2 depends on strict-concurrency-0.2.3 which failed to
install.
happstack-util-0.5.0.2 depends on strict-concurrency-0.2.3 which failed to
install.
haskell-src-exts-1.9.0 failed during the configure step. The exception was:
ExitFailure 1
hsp-0.5.2 depends on haskell-src-exts-1.9.0 which failed to install.
hsx-0.7.0 depends on haskell-src-exts-1.9.0 which failed to install.
strict-concurrency-0.2.3 failed during the building phase. The exception was:
ExitFailure 1

んー。どうも、strict-concurrency-0.2.3とやらがうまく入らないみたいだが、原因は分からず。直接落としてきても同じエラーが出たので、ここで詰み。ググってみたら、debianにはaptで取ってこれるhappstackがあるっぽいので、debianを入れてみようかな(今まで作業してたのはubuntu)。
で、どうせならWASHも試してみることに。

$ sh ./bootstrap.sh
$ wget http://www.informatik.uni-freiburg.de/~thiemann/WASH/WashNGo-2.12.tgz
$ tar xzvf WashNGo-2.12.tgz
$ cd WashNGo-2.12/
$ cabal install
Resolving dependencies...
Configuring WashNGo-2.12...
Warning: No 'build-type' specified. If you do not need a custom Setup.hs or
./configure script then use 'build-type: Simple'.
Preprocessing library WashNGo-2.12...
Preprocessing executables for WashNGo-2.12...
Building WashNGo-2.12...

WASH/CGI/CGIMonad.hs:1:15:
    Warning: -fno-monomorphism-restriction is deprecated: use -XNoMonomorphismRestriction or pragma {-# LANGUAGE NoMonomorphismRestriction#-} instead

WASH/CGI/SubmitXX.hs:1:15:
    Warning: -fallow-undecidable-instances is deprecated: use -XUndecidableInstances or pragma {-# LANGUAGE UndecidableInstances#-} instead

WASH/CGI/CGIHistory.hs:10:17:
    Could not find module `Data.Map':
      it is a member of the hidden package `containers-0.2.0.1'
      Use -v to see a list of the files searched for.
cabal: Error: some packages failed to install:
WashNGo-2.12 failed during the building phase. The exception was:
ExitFailure 1

うんまあ、こっちもなんかダメみたいだ…。取りあえず、今日はここまで。debianVM作ってみよう。

今日の企画

上記の情報を管理できるシステムの構築。最初は日記でやろうかと思ったが、これは公開する内容じゃないし、専用システムがあったほうが、情報管理も楽だろうと思う。メール投稿等の機能も欲しいところ。ただし、システムが構築できるまでは、excel管理とする。

今後のこと

誰がどう言っても、やっぱ技術でいくしかないんじゃないか思う。最悪、フリーとかで生きていけるくらいは必要。プラス企画が出来れば、安心か。会社にいたら、そこで使う技術しか身に付かない。当たり前です。なんで、他の技術を身に付けようと思ったら会社にいる時間を最小化しないといけない。幸い今のPLからは、早く帰ること自体は構わないと言ってもらった。今後、たとえば早く帰ることを許可しない上司の下に付いたら会社を辞めることを検討しなければならないし、会社で得られる技術にすごく価値があるのであれば、長く会社にいることも検討していいと思う。
早く帰るために必要なのは、その日やらないといけないことを明確に洗い出すこと。後は自己管理が出来れば、ほとんど定時退社できたようなもの。さらに言うと、多少の図々しさ(を実践するためのMP)が必要になるが、これは自分内コアコンピタンスが明確になっていれば、さほど気にならないものと思う。以上をアクションに落とすと・・・

  • 起床、出社、退社、就寝時刻のメモ
  • 起床後、就寝前にやったこと

あたりを管理していけばいいことになるか。プラスアルファとして、

  • 早起きをする
  • 仕事上、アイデアを出して実践したことのメモ
  • その日のパフォーマンス
  • その他、反省点等
  • 体重
  • 今日の企画
  • 今日の新知識

くらいが考えられるか。あとは脳の疲労を測定するためのパラメータが欲しいところだが、ここは検討事項としておこうと思う。他に思いついたことがあれば、適宜追記していこうと思う。あと、これを公表する必要は無いか・・・。
今日の企画というのは、毎日アイデアを出すようにして、それを積み立てていけば、いずれ起業か何かの役に立つだろう、という案。