メインコンテンツまでスキップ

チャット、入力、イベント

チャットを送受信する

import { onMessage, reply, send } from "@urth/metatell-sdk/chat";

const unsubscribe = onMessage((message) => {
console.log(message.from.displayName, message.text);
});

try {
const sent = await send("こんにちは");
await reply(sent.id, "返信です");
} catch (error) {
console.error("チャットの送信または返信に失敗しました", error);
}

send()の既定チャンネルはglobalです。 groupへ送る場合はgroupIdも指定します。 send()reply()は、ホスト未接続、ホスト側のエラー、タイムアウトなどで失敗するため、例外を処理してください。

await send("グループ向け", {
channel: "group",
groupId: "group-id",
timeoutMs: 5000,
});

メンション文字列はparseMentions()で解析できます。

import { parseMentions } from "@urth/metatell-sdk/chat";

const mentions = parseMentions("こんにちは [@Alice](a1b2c3d4)");

ポインターと視線を購読する

import {
onGaze,
onHover,
onPointerDown,
onPointerMove,
onPointerUp,
} from "@urth/metatell-sdk/input";

const unsubscribers = [
onPointerDown((event) => console.log(event.objectId)),
onPointerUp((event) => console.log(event.point)),
onPointerMove((event) => console.log(event.kind)),
onHover((event) => console.log(event.objectId)),
onGaze((info) => console.log(info.objectId)),
];

onPointerMove()onHover()は高頻度で呼ばれるため、ハンドラーを軽量に保ってください。

Raycastを行う

import { raycast } from "@urth/metatell-sdk/input";

const hit = await raycast(
{ x: 0, y: 1.6, z: 0 },
{ x: 0, y: 0, z: -1 },
{ maxDistance: 10, timeoutMs: 5000 },
);

if (hit) {
console.log(hit.objectId, hit.distance, hit.point);
}

何にも当たらなかった場合はnullを返します。 ホスト未接続、ホスト側のエラー、タイムアウトではPromiseが失敗します。

低レベルのイベントバス

専用モジュールにないイベントが必要な場合は、@urth/metatell-sdk/eventsを利用できます。

import { off, on, once } from "@urth/metatell-sdk/events";

const handler = (message) => console.log(message.text);
const unsubscribe = on("chat:message", handler);

once("users:join", (user) => {
console.log(user.profile.displayName);
});

unsubscribe();
off("chat:message", handler);

getBus()は共有イベントバスを返します。 createBus()はテストなどで独立したイベントバスが必要な場合に使います。

通常は、型と失敗処理が整ったchatusersinputなどの専用APIを優先してください。