本文整理汇总了TypeScript中mock-socket.Server类的典型用法代码示例。如果您正苦于以下问题:TypeScript Server类的具体用法?TypeScript Server怎么用?TypeScript Server使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Server类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: before
before(function () {
const fakeURL = 'ws://localhost:8080/peerjs?key=peerjs&id=1&token=testToken';
mockServer = new Server(fakeURL);
mockServer.on('connection', socket => {
//@ts-ignore
socket.on('message', data => {
socket.send('test message from mock server');
});
socket.send(ServerMessageType.Open);
});
});
示例2: it
it('should connect', (done) => {
mockwebSocketServer.on('connection', () => {
done();
});
dotEventsSocket.connect().subscribe(() => {});
});
示例3: it
it('should send a request', done => {
const brayns = new Client(host);
mockServer.on('connection', socket => {
// TODO: (socket as any) is due to https://github.com/thoov/mock-socket/issues/224,
// remove when fixed
(socket as any).on('message', (data: any) => {
const json = fromJson<JsonRpcRequest>(data);
expect(isJsonRpcRequest(json)).toBe(true);
expect(json.id).toBeDefined();
expect(json.method).toBe(INSPECT);
expect(json.params).toEqual({
ping: true
});
const response = toResponseJson(json, true);
socket.send(response);
});
});
brayns.request(INSPECT, {ping: true} as any)
.then(() => {
done();
});
});
示例4: callback
const addServerHandler = (
method: string,
path: string,
responseData: Omit<RequestSuccessResponse, 'callback_id'> | Omit<RequestErrorResponse, 'callback_id'>,
callback: Callback
) => {
const handler = (jsonRequest: string) => {
const requestObj: OutgoingRequest = JSON.parse(jsonRequest);
if (requestObj.path !== path || requestObj.method !== method) {
//console.log(requestObj, requestObj.path, path);
return;
}
if (callback) {
callback(requestObj);
}
const response: RequestSuccessResponse | RequestErrorResponse = {
callback_id: requestObj.callback_id,
...responseData,
};
mockServer.send(JSON.stringify(response));
};
mockServer.addEventListener('message', handler);
};
示例5:
mockServer.on('connection', socket => {
mockServer.close({
code: 1005,
reason: 'Why not?',
wasClean: false
});
});
示例6: describe
describe('{ready}', () => {
let mockServer: Server;
beforeEach(() => {
mockServer = new Server(wsHost);
});
afterEach(done => {
mockServer.stop(done);
});
it('should emit false by default', done => {
const brayns = new Client(host);
brayns.ready.pipe(take(1))
.subscribe(ready => {
expect(ready).toBe(false);
done();
});
});
it('should emit true when the socket establishes a connection with the ws server', done => {
const brayns = new Client(host);
brayns.ready.pipe(skip(1), take(1))
.subscribe(ready => {
expect(ready).toBe(true);
done();
});
});
it('should emit false when the socket connection with the ws server is closed', done => {
const brayns = new Client(host);
brayns.ready.pipe(skip(2), take(1))
.subscribe(ready => {
expect(ready).toBe(false);
done();
});
mockServer.on('connection', socket => {
socket.close(0, 'Why not?');
});
});
it('should emit false when the socket connection with the ws server errored', done => {
const brayns = new Client(host);
brayns.ready.pipe(skip(2), take(1))
.subscribe(ready => {
expect(ready).toBe(false);
done();
});
mockServer.on('connection', socket => {
mockServer.close({
code: 1005,
reason: 'Why not?',
wasClean: false
});
});
});
});
示例7: beforeEach
beforeEach(async () => {
mockServer = new Server("ws://localhost:6868");
p = new Promise((resolve, reject) => { res = resolve; rej = reject; });
const serverInfo = (jsonmsg: string) => {
const msg: Messages.ButtplugMessage = FromJSON(jsonmsg)[0] as Messages.ButtplugMessage;
if (msg.Type === Messages.RequestServerInfo) {
delaySend(new Messages.ServerInfo(0, 0, 0, 1, 0, "Test Server", msg.Id));
}
if (msg.Type === Messages.RequestDeviceList) {
delaySend(new Messages.DeviceList([], msg.Id));
// (socket as any).removeListener("message", serverInfo);
}
};
mockServer.on("connection", (aSocket: WebSocket) => {
socket = aSocket;
// TODO Bug in typescript defs for mock-socket 8 means we can't use the
// socket type as it was meant. See
// https://github.com/thoov/mock-socket/issues/224
(socket as any).on("message", (data: string) => serverInfo(data));
});
bp = new ButtplugClient("Test Buttplug Client");
await bp.Connect(new ButtplugBrowserWebsocketClientConnector("ws://localhost:6868"));
});