package service

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"iot_manager_service/util/websocket"
	"net"
	"strconv"
)

type server struct {
	UnimplementedSendMessageServer
}

func (s server) Distribution(ctx context.Context, req *DistributionRequest) (*DistributionResponse, error) {
	for _, user := range req.Users {
		websocket.SendMessage(strconv.FormatInt(user, 10), "您有新的文件，请注意查收！")
	}
	return &DistributionResponse{Code: 200}, nil
}

func InitGrpcServer() {
	//开启端口监听
	listen, _ := net.Listen("tcp", "127.0.0.1:9090")
	//创建grpc服务
	grpcServer := grpc.NewServer()
	//注册服务
	RegisterSendMessageServer(grpcServer, &server{})

	//启动服务
	err := grpcServer.Serve(listen)
	if err != nil {
		fmt.Printf("failed to serve: %v", err)
		return
	}
}
