41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import * as grpcWeb from "grpc-web"
|
|
import { SubscribeRequest, SubscribeResponse } from './proto/gameobject';
|
|
import { UpdaterClient } from "./proto/gameobject.client"
|
|
import { ServerStreamingCall } from "@protobuf-ts/runtime-rpc"
|
|
import { GrpcWebFetchTransport } from "@protobuf-ts/grpcweb-transport";
|
|
|
|
class Updater {
|
|
static readonly INTERVAL = 500; // ms
|
|
static readonly MAX_STREAM_MESSAGES = 50;
|
|
|
|
stream: ServerStreamingCall<SubscribeRequest, SubscribeResponse>;
|
|
|
|
constructor(public updaterService: UpdaterClient, uuid: string) {
|
|
// Subscribe to server
|
|
this.stream = this.updaterService.subscribeCreations({ uuid })
|
|
//Handle server stream
|
|
this.stream.responses.onNext((a) => {
|
|
console.log(a)
|
|
})
|
|
this.stream.responses.onMessage((response: SubscribeResponse) => {
|
|
console.log(response.uuid)
|
|
});
|
|
this.stream.responses.onError((err) => {
|
|
console.error(err.message)
|
|
});
|
|
this.stream.responses.onComplete(() => {
|
|
console.log('stream end signal received');
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
const transport = new GrpcWebFetchTransport({
|
|
baseUrl: "http://localhost:8080"
|
|
});
|
|
|
|
const updaterService = new UpdaterClient(transport);
|
|
|
|
export function subscribeObject(uuid: string) {
|
|
return new Updater(updaterService, uuid)
|
|
} |