1 Star 0 Fork 0

yuliang87 / websocket

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.go 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
yuliang87 提交于 2021-09-05 13:50 . 1
package main
import (
"context"
"errors"
"log"
"net"
"net/http"
"os"
"os/signal"
"time"
)
func main() {
log.SetFlags(0)
err := run()
if err != nil {
log.Fatal(err)
}
}
// run initializes the chatServer and then
// starts a http.Server for the passed in address.
func run() error {
if len(os.Args) < 2 {
return errors.New("please provide an address to listen on as the first argument")
}
l, err := net.Listen("tcp", os.Args[1])
if err != nil {
return err
}
log.Printf("listening on http://%v", l.Addr())
cs := newChatServer()
s := &http.Server{
Handler: cs,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
}
errc := make(chan error, 1)
go func() {
errc <- s.Serve(l)
}()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
select {
case err := <-errc:
log.Printf("failed to serve: %v", err)
case sig := <-sigs:
log.Printf("terminating: %v", sig)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
return s.Shutdown(ctx)
}
1
https://gitee.com/yuliang_team/websocket.git
git@gitee.com:yuliang_team/websocket.git
yuliang_team
websocket
websocket
64f9d9db21fb

搜索帮助