Skip to content

Commit 2cfb765

Browse files
authored
Merge pull request #226 from online-go/web-worker-socket
Create an interface for our GobanSocket
2 parents b756aec + 55c23f2 commit 2cfb765

File tree

5 files changed

+45
-12
lines changed

5 files changed

+45
-12
lines changed

src/Goban/OGSConnectivity.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
deepEqual,
3030
deepClone,
3131
encodeMove,
32-
GobanSocket,
32+
IGobanSocket,
3333
GobanSocketEvents,
3434
ConditionalMoveTree,
3535
GobanEngine,
@@ -67,7 +67,7 @@ interface JGOFPlayerClockWithTimedOut extends JGOFPlayerClock {
6767
*/
6868
export abstract class OGSConnectivity extends GobanInteractive {
6969
public sent_timed_out_message: boolean = false;
70-
protected socket!: GobanSocket;
70+
protected socket!: IGobanSocket;
7171
protected socket_event_bindings: Array<[keyof GobanSocketEvents, () => void]> = [];
7272
protected connectToReviewSent?: boolean;
7373

@@ -134,7 +134,7 @@ export abstract class OGSConnectivity extends GobanInteractive {
134134
return 0;
135135
}
136136

137-
protected connect(server_socket: GobanSocket): void {
137+
protected connect(server_socket: IGobanSocket): void {
138138
const socket = (this.socket = server_socket);
139139

140140
this.disconnectedFromGame = false;

src/GobanBase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
} from "./engine/formats/JGOF";
4040
import { AdHocPackedMove } from "./engine/formats/AdHocFormat";
4141
import { MessageID } from "./engine/messages";
42-
import type { GobanSocket } from "./engine/GobanSocket";
42+
import type { IGobanSocket } from "./engine/GobanSocket";
4343
import type { ServerToClient, GameChatLine } from "./engine/protocol";
4444
import { EventEmitter } from "eventemitter3";
4545
import { setGobanCallbacks } from "./Goban/callbacks";
@@ -127,7 +127,7 @@ export interface GobanConfig extends GobanEngineConfig, PuzzleConfig {
127127

128128
// deprecated
129129
username?: string;
130-
server_socket?: GobanSocket;
130+
server_socket?: IGobanSocket;
131131
connect_to_chat?: number | boolean;
132132
}
133133

src/engine/GobanSocket.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ interface ErrorResponse {
4141
message: string;
4242
}
4343

44-
interface GobanSocketOptions {
44+
export interface GobanSocketOptions {
4545
/** Don't automatically send pings */
4646
dont_ping?: boolean;
4747

@@ -67,6 +67,37 @@ const DEFAULT_PING_INTERVAL = 10000;
6767
export type DataArgument<Entry> = Entry extends (...args: infer A) => void ? A[0] : never;
6868
export type ProtocolResponseType<Entry> = Entry extends (...args: any[]) => infer R ? R : never;
6969

70+
/**
71+
* Interface describing the public API of a GobanSocket (or compatible proxy).
72+
* Both GobanSocket and GobanSocketProxy satisfy this interface.
73+
* Extends EventEmitter<GobanSocketEvents> so on/off/emit types match exactly.
74+
*/
75+
export interface IGobanSocket<
76+
SendProtocol extends ClientToServerBase = ClientToServer,
77+
RecvProtocol = ServerToClient,
78+
> extends EventEmitter<GobanSocketEvents> {
79+
readonly url: string;
80+
clock_drift: number;
81+
latency: number;
82+
options: GobanSocketOptions;
83+
readonly connected: boolean;
84+
85+
send<Command extends keyof SendProtocol>(
86+
command: Command,
87+
data: DataArgument<SendProtocol[Command]>,
88+
cb?: (data: ProtocolResponseType<SendProtocol[Command]>, error?: any) => void,
89+
): void;
90+
91+
sendPromise<Command extends keyof SendProtocol>(
92+
command: Command,
93+
data: DataArgument<SendProtocol[Command]>,
94+
): Promise<ProtocolResponseType<SendProtocol[Command]>>;
95+
96+
authenticate(authentication: DataArgument<SendProtocol["authenticate"]>): void;
97+
disconnect(): void;
98+
ping(): void;
99+
}
100+
70101
/**
71102
* This is a simple wrapper around the WebSocket API that provides a
72103
* simple interface to connect to the Online-Go.com servers. It provides:

src/engine/ai/AIReviewData.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { EventEmitter } from "eventemitter3";
18-
import { GobanSocket, GobanSocketEvents } from "../GobanSocket";
18+
import { IGobanSocket, GobanSocketEvents } from "../GobanSocket";
1919
import { JGOFAIReview, JGOFAIReviewMove } from "../formats/JGOF";
2020
import * as protocol from "../protocol";
2121
import { MoveTree } from "../MoveTree";
@@ -52,14 +52,14 @@ export interface AIReviewDataEvents {
5252
* Takes care of socket communication and updating AI review data as it streams in.
5353
* */
5454
export class AIReviewData extends EventEmitter<AIReviewDataEvents> implements JGOFAIReview {
55-
public readonly socket: GobanSocket<protocol.ClientToAIServer, protocol.AIServerToClient>;
55+
public readonly socket: IGobanSocket<protocol.ClientToAIServer, protocol.AIServerToClient>;
5656
public readonly uuid: string;
5757
private ai_review: JGOFAIReview;
5858
public readonly move_tree: MoveTree;
5959
private analysis_requests_made: { [id: string]: boolean } = {};
6060

6161
constructor(
62-
socket: GobanSocket<protocol.ClientToAIServer, protocol.AIServerToClient>,
62+
socket: IGobanSocket<protocol.ClientToAIServer, protocol.AIServerToClient>,
6363
move_tree: MoveTree,
6464
ai_review: JGOFAIReview,
6565
game_id: number | string,

test/unit_tests/AIReviewData.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@
1717
(global as any).CLIENT = true;
1818

1919
import WS from "jest-websocket-mock";
20-
import { GobanSocket } from "../../src/engine/GobanSocket";
20+
import { GobanSocket, IGobanSocket } from "../../src/engine/GobanSocket";
2121
import { AIReviewData } from "../../src/engine/ai/AIReviewData";
22+
import type { ClientToAIServer } from "../../src/engine/protocol/ClientToAIServer";
23+
import type { AIServerToClient } from "../../src/engine/protocol/AIServerToClient";
2224
import { JGOFAIReview } from "../../src/engine/formats/JGOF";
2325
import { GobanEngine } from "../../src/engine/GobanEngine";
2426

2527
describe("AIReviewData", () => {
2628
let socket_server: WS;
27-
let mock_socket: GobanSocket<any, any>;
29+
let mock_socket: IGobanSocket<ClientToAIServer, AIServerToClient>;
2830
const port = 48890;
2931

3032
beforeEach(async () => {
3133
socket_server = new WS(`ws://localhost:${port}`, { jsonProtocol: true });
32-
mock_socket = new GobanSocket(`ws://localhost:${port}`, {
34+
mock_socket = new GobanSocket<ClientToAIServer, AIServerToClient>(`ws://localhost:${port}`, {
3335
dont_ping: true,
3436
quiet: true,
3537
});

0 commit comments

Comments
 (0)