Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed SongServer to use interface types. #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as grpc from '@grpc/grpc-js';
import { sendUnaryData } from '@grpc/grpc-js/build/src/server-call';
import { Empty } from 'google-protobuf/google/protobuf/empty_pb';
import { Song, Comment } from '../proto/songs_pb';
import { ISongsServer, SongsService } from '../proto/songs_grpc_pb';
Expand All @@ -8,41 +7,41 @@ import addSong from './add-song';
import getChat from './get-chat';
import { addComment, registerListener } from './live-chat';

class SongsServer implements ISongsServer {
getSong(call: grpc.ServerUnaryCall<Empty, Song>, callback: sendUnaryData<Song>): void {
const createSongsServer = (): ISongsServer => ({
getSong(_call, callback): void {
console.log(`${new Date().toISOString()} getSong`);
callback(null, getSong());
}
addSongs(call: grpc.ServerReadableStream<Song, Empty>, callback: sendUnaryData<Empty>): void {
},
addSongs(call, callback): void {
console.log(`${new Date().toISOString()} addSongs`);
call.on('data', (song: Song) => {
addSong(song);
});
call.on('end', () => callback(null, new Empty()));
}
async getChat(call: grpc.ServerWritableStream<Song, Comment>): Promise<void> {
},
async getChat(call): Promise<void> {
console.log(`${new Date().toISOString()} getChat`);
const song = call.request as Song;
const comments = await getChat(song.getId());
for (const comment of comments) {
call.write(comment);
}
call.end();
}
liveChat(call: grpc.ServerDuplexStream<Comment, Comment>): void {
},
liveChat(call): void {
console.log(`${new Date().toISOString()} liveChat`);
registerListener((comment) => call.write(comment));
call.on('data', (comment: Comment) => {
addComment(comment);
});
call.on('end', () => call.end());
}
}
});

function serve(): void {
const server = new grpc.Server();
// @ts-ignore
server.addService(SongsService, new SongsServer());
server.addService(SongsService, createSongsServer());
server.bindAsync(`localhost:${process.env.PORT}`, grpc.ServerCredentials.createInsecure(), (err, port) => {
if (err) {
throw err;
Expand Down