Answer the question
In order to leave comments, you need to log in
Go socket wild brakes 5 connections how so?
In general, the socket launches a command every second in Laravel, but it instantly works out less than 300ms.
Receives data from memcache and pushes them to the channels.
Server VDS 4gb RAM 2 cores, it can't fail because of 5 connections like this
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
ReadBufferSize: 8192,
WriteBufferSize: 8192,
}
var clients = make(map[*websocket.Conn]bool)
var mc = memcache.New("127.0.0.1:11211")
func echoHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
clients[conn] = true
mapD := map[string]interface{}{"user": len(clients)}
mapB, _ := json.Marshal(mapD)
for {
for client := range clients {
ErrorSendOnline := client.WriteMessage(1, mapB)
if ErrorSendOnline != nil {
client.Close()
delete(clients, client)
}
}
messageType, p, err := conn.ReadMessage()
if err != nil {
return
}
err = conn.WriteMessage(messageType, p)
if err != nil {
return
}
}
}
func getCache() {
c := time.Tick(1000 * time.Millisecond)
for range c {
exec.Command("php", "/var/www/elrush/artisan", "commandMe").Run()
}
}
func getData() {
c := time.Tick(1000 * time.Millisecond)
for range c {
if len(clients) > 0 {
val, errorMc := mc.Get("tem")
for client := range clients {
if errorMc == nil {
errorSendCache := client.WriteMessage(1, val.Value)
if errorSendCache != nil {
client.Close()
delete(clients, client)
}
mapD := map[string]interface{}{"user": len(clients)}
mapB, _ := json.Marshal(mapD)
ErrorSendOnline := client.WriteMessage(1, mapB)
if ErrorSendOnline != nil {
client.Close()
delete(clients, client)
}
}
}
if errorMc == nil {
mc.Delete("item")
}
}
}
func main() {
http.HandleFunc("/", echoHandler)
go getCache() //горутина
go getData() //горутина
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic("Error: " + err.Error())
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question