{"id":15532055,"url":"https://github.com/togettoyou/wsc","last_synced_at":"2025-04-23T13:19:44.729Z","repository":{"id":56392193,"uuid":"311642892","full_name":"togettoyou/wsc","owner":"togettoyou","description":"🏓 golang websocket client 基于 gorilla/websocket 具有断线重连机制的 websocket go 客户端实现","archived":false,"fork":false,"pushed_at":"2021-02-05T09:17:03.000Z","size":29,"stargazers_count":47,"open_issues_count":2,"forks_count":16,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-23T13:19:38.521Z","etag":null,"topics":["go","golang-websocket-client","gorilla-websocket"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/togettoyou.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-10T11:52:21.000Z","updated_at":"2024-12-12T16:39:53.000Z","dependencies_parsed_at":"2022-08-15T17:50:47.412Z","dependency_job_id":null,"html_url":"https://github.com/togettoyou/wsc","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/togettoyou%2Fwsc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/togettoyou%2Fwsc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/togettoyou%2Fwsc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/togettoyou%2Fwsc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/togettoyou","download_url":"https://codeload.github.com/togettoyou/wsc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250439296,"owners_count":21430825,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang-websocket-client","gorilla-websocket"],"created_at":"2024-10-02T11:28:57.981Z","updated_at":"2025-04-23T13:19:44.708Z","avatar_url":"https://github.com/togettoyou.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# golang websocket client\n[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white\u0026style=flat-square)](https://pkg.go.dev/github.com/togettoyou/wsc)\n[![GoDoc](https://godoc.org/github.com/togettoyou/wsc?status.svg)](https://godoc.org/github.com/togettoyou/wsc)\n\n### Install\n\n```\n$ go get -v github.com/togettoyou/wsc\n```\n#### Simple example\n\n``` go\npackage main\n\nimport (\n\t\"github.com/togettoyou/wsc\"\n\t\"log\"\n\t\"time\"\n)\n\nfunc main() {\n\tdone := make(chan bool)\n\tws := wsc.New(\"ws://127.0.0.1:7777/ws\")\n\t// 可自定义配置，不使用默认配置\n\t//ws.SetConfig(\u0026wsc.Config{\n\t//\t// 写超时\n\t//\tWriteWait: 10 * time.Second,\n\t//\t// 支持接受的消息最大长度，默认512字节\n\t//\tMaxMessageSize: 2048,\n\t//\t// 最小重连时间间隔\n\t//\tMinRecTime: 2 * time.Second,\n\t//\t// 最大重连时间间隔\n\t//\tMaxRecTime: 60 * time.Second,\n\t//\t// 每次重连失败继续重连的时间间隔递增的乘数因子，递增到最大重连时间间隔为止\n\t//\tRecFactor: 1.5,\n\t//\t// 消息发送缓冲池大小，默认256\n\t//\tMessageBufferSize: 1024,\n\t//})\n\t// 设置回调处理\n\tws.OnConnected(func() {\n\t\tlog.Println(\"OnConnected: \", ws.WebSocket.Url)\n\t\t// 连接成功后，测试每5秒发送消息\n\t\tgo func() {\n\t\t\tt := time.NewTicker(5 * time.Second)\n\t\t\tfor {\n\t\t\t\tselect {\n\t\t\t\tcase \u003c-t.C:\n\t\t\t\t\terr := ws.SendTextMessage(\"hello\")\n\t\t\t\t\tif err == wsc.CloseErr {\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}()\n\t})\n\tws.OnConnectError(func(err error) {\n\t\tlog.Println(\"OnConnectError: \", err.Error())\n\t})\n\tws.OnDisconnected(func(err error) {\n\t\tlog.Println(\"OnDisconnected: \", err.Error())\n\t})\n\tws.OnClose(func(code int, text string) {\n\t\tlog.Println(\"OnClose: \", code, text)\n\t\tdone \u003c- true\n\t})\n\tws.OnTextMessageSent(func(message string) {\n\t\tlog.Println(\"OnTextMessageSent: \", message)\n\t})\n\tws.OnBinaryMessageSent(func(data []byte) {\n\t\tlog.Println(\"OnBinaryMessageSent: \", string(data))\n\t})\n\tws.OnSentError(func(err error) {\n\t\tlog.Println(\"OnSentError: \", err.Error())\n\t})\n\tws.OnPingReceived(func(appData string) {\n\t\tlog.Println(\"OnPingReceived: \", appData)\n\t})\n\tws.OnPongReceived(func(appData string) {\n\t\tlog.Println(\"OnPongReceived: \", appData)\n\t})\n\tws.OnTextMessageReceived(func(message string) {\n\t\tlog.Println(\"OnTextMessageReceived: \", message)\n\t})\n\tws.OnBinaryMessageReceived(func(data []byte) {\n\t\tlog.Println(\"OnBinaryMessageReceived: \", string(data))\n\t})\n\t// 开始连接\n\tgo ws.Connect()\n\tfor {\n\t\tselect {\n\t\tcase \u003c-done:\n\t\t\treturn\n\t\t}\n\t}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftogettoyou%2Fwsc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftogettoyou%2Fwsc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftogettoyou%2Fwsc/lists"}