sphero-websocket v0.5.14
sphero-websocket
使い方
サーバ
モジュールをインストール
$ npm install --save sphero-websocket設定ファイルを作成
カレントディレクトリに、設定ファイルsphero-ws-config.jsを作成する。
例:
module.exports = {
wsPort: 8080,
allowedOrigin: "*",
sphero: [
{name: "Miku", port: "/dev/rfcomm0"},
{name: "Rin", port: "/dev/rfcomm1"}
]
};- wsPort サーバのポート番号
- allowedOrigin
接続を許可するoriginを指定する。
*ならば全て許可。複数指定はArrayで与える。 - Sphero
サーバに接続するSpheroを列挙する。
- name 識別名(省略可)
- port 接続ポート
サーバーを起動する
sphero-ws-config.jsがあるディレクトリにて実行。
$ "./node-modules/.bin/sphero-websocket"package.jsonのscriptsを次のようにすると、いい感じになる。
"scripts": {
"server": "sphero-websocket",
"server-test": "sphero-websocket --test"
}次のようにできるようになる。
$ npm run server
もしくは、--test オプションをつけて実行
$ npm run server-test--testオプションを付けると、実際にデバイスには接続せずログ出力のみとなる。
クライアント
ここから先、ポート番号は、8080を使っていることとする。
sphero-ws-config.js でポート番号を変更することができる。
example
(Client側ライブラリ)sphero-client.js
読み込みは、次のようにしてできる。
<script src="http://localhost:8080/client/sphero-client.js"></script>以下のような感じで、Sphero.jsっぽく使える。
var orb = new sphero();
orb.connect("ws://127.0.0.1:8080", function() {
orb.color("FF00FF");
orb.roll(100, 0);
});API
Spheroのコマンド(API)については、Sphero.js(JavaScript API Doc)を参照。
以下のコマンドが叩けるようになっている。Data Streaming系のコマンドとか、レスポンスを返すのは未実装。
- sphero.js/lib/devices/sphero.js
- sphero.js/lib/devices/custom.js
orb.connect(uri, successCallback, errorCallback)
uriは接続先WebSocketサーバのURIsuccessCallbackとerrorCallbackは、それぞれ接続成功、接続失敗時に呼び出される。
orb.getList(callback)
サーバに接続されているSpheroの名前のリストを返す。callbackの引数にSpheroの名前のArrayが渡される。
orb.use(name)
使うSpheroを設定する。nameに使いたいSpheroの名前を指定する。
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago