This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto3"; | |
option go_package = "./chatpb"; | |
package chatpb; | |
service ChatService { | |
rpc JoinChannel(Channel) returns (stream Message) {} | |
rpc SendMessage(stream Message) returns (MessageAck) {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (s *chatServiceServer) JoinChannel(ch *chatpb.Channel, msgStream chatpb.ChatService_JoinChannelServer) error { | |
msgChannel := make(chan *chatpb.Message) | |
s.channel[ch.Name] = append(s.channel[ch.Name], msgChannel) | |
// doing this never closes the stream | |
for { | |
select { | |
case <-msgStream.Context().Done(): | |
return nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (s *chatServiceServer) SendMessage(msgStream chatpb.ChatService_SendMessageServer) error { | |
msg, err := msgStream.Recv() | |
if err == io.EOF { | |
return nil | |
} | |
if err != nil { | |
return err | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lis, err := net.Listen("tcp", "localhost:5400") | |
if err != nil { | |
log.Fatalf("Failed to listen: %v", err) | |
} | |
var opts []grpc.ServerOption | |
grpcServer := grpc.NewServer(opts...) | |
chatpb.RegisterChatServiceServer(grpcServer, &chatServiceServer{ | |
channel: make(map[string][] chan *chatpb.Message), | |
} | |
grpcServer.Serve(lis) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func joinChannel(ctx context.Context, client chatpb.ChatServiceClient) { | |
channel := chatpb.Channel{Name: *channelName, SendersName: *senderName} | |
stream, err := client.JoinChannel(ctx, &channel) | |
if err != nil { | |
log.Fatalf("client.JoinChannel(ctx, &channel) throws: %v", err) | |
} | |
fmt.Printf("Joined channel: %v \n", *channelName) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func sendMessage(ctx context.Context, client chatpb.ChatServiceClient, message string) { | |
stream, err := client.SendMessage(ctx) | |
if err != nil { | |
log.Printf("Cannot send message: error: %v", err) | |
} | |
msg := chatpb.Message{ | |
Channel: &chatpb.Channel{ | |
Name: *channelName, | |
SendersName: *senderName}, | |
Message: message, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var channelName = flag.String("channel", "default", "Channel name for chatting") | |
var senderName = flag.String("sender", "default", "Senders name") | |
var tcpServer = flag.String("server", ":5400", "Tcp server") | |
func main() { | |
flag.Parse() | |
fmt.Println("--- CLIENT APP ---") | |
var opts []grpc.DialOption |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from "react"; | |
/** | |
* Use Buffered State gives buffer functionality to the state. | |
* Adding elements more than the buffered state length will add | |
* to the first of the state and old values will be deleted from state. | |
* | |
* ##### NOTE | |
* - https://stackoverflow.com/questions/55265255/react-usestate-hook-event-handler-using-initial-state | |
* There is use of callback function in setState so that current state can be accessed inside event listner instead of |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useRef, useEffect } from "react"; | |
import Chart from "chart.js"; | |
const barChartData: Chart.ChartData = { | |
labels: ["January", "February", "March", "April", "May", "June", "July"], | |
datasets: [ | |
{ | |
label: "Male", | |
stack: "Stack 0", | |
backgroundColor: "#d41111", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ADecorator = <T extends { new(...args: any[]): {} }>(specs: T) => { | |
return class extends specs { | |
A = "test" | |
constructor(...args: any[]) { | |
super(...args) | |
console.log("object constructed") | |
} | |
} | |
} |
OlderNewer