WorldWebSocketStart.go 4.72 KB
Newer Older
Ford committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package service

import (
	//"chat/conf"
	"WorldEpcho/src/config/e"
	"WorldEpcho/src/utils"
	"encoding/json"
	"fmt"
	_ "fmt"
	//"github.com/gin-gonic/gin/binding"

	"github.com/gorilla/websocket"
	"log"
)

16
func (manager *WorldClientManager) WorldWebSocketStart_bak() {
Ford committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
	for {
		log.Println("<---监听管道通信--->")
		select {
		case conn := <-WorldManager.Register: // 建立连接
			log.Printf("建立新连接: %v", conn.WorldConversations.Uid)
			if conn == nil || conn.Socket == nil {
				log.Printf("无效的连接或socket为空")
				continue // Skip this iteration if connection or socket is nil
			}
			WorldManager.Client[utils.Strval(conn.WorldConversations.Uid)] = conn
			replyMsg := &WorldReplyMsg{
				StatusCode: e.SUCCESS,
				Code:       e.WebsocketSuccess,
				Content:    "已连接至服务器",
			}
32

Ford committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
			fmt.Println("WorldManager.Client: ", WorldManager.Client)
			msg, err := json.Marshal(replyMsg)
			if err != nil {
				log.Printf("消息序列化失败: %v", err)
				continue
			}
			err = conn.Socket.WriteMessage(websocket.TextMessage, msg)
			if err != nil {
				log.Printf("消息发送失败: %v", err)
				continue
			}

		case conn := <-WorldManager.Unregister: // 断开连接
			log.Printf("连接失败:%v", conn.WorldConversations.Uid)
			if conn == nil || conn.Socket == nil {
				log.Printf("无效的连接或socket为空")
				continue // Skip this iteration if connection or socket is nil
			}
			if _, ok := WorldManager.Client[utils.Strval(conn.WorldConversations.Uid)]; ok {
				replyMsg := &WorldReplyMsg{
					Code:    e.WebsocketEnd,
					Content: "连接已断开",
				}
				msg, err := json.Marshal(replyMsg)
				if err != nil {
					log.Printf("消息序列化失败: %v", err)
					continue
				}
				err = conn.Socket.WriteMessage(websocket.TextMessage, msg)
				if err != nil {
					log.Printf("消息发送失败: %v", err)
					continue
				}
66 67 68
				if conn.Send != nil {
					close(conn.Send)
				}
Ford committed
69 70 71 72 73
				delete(WorldManager.Client, utils.Strval(conn.WorldConversations.Uid))
			}
		}
	}
}
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

func (manager *WorldClientManager) WorldWebSocketStart() {
	for {
		log.Println("<---监听管道通信--->")
		select {
		case conn := <-WorldManager.Register: // 建立连接
			log.Printf("建立新连接: %v", conn.WorldConversations.Uid)
			if conn == nil || conn.Socket == nil {
				log.Printf("无效的连接或socket为空")
				continue // 如果连接或socket为空,跳过这次循环
			}
			// 将客户端添加到管理器
			WorldManager.Client[utils.Strval(conn.WorldConversations.Uid)] = conn

			// 异步启动一个 Goroutine 处理 WebSocket 消息发送,提升并发性能
			go func(conn *WorldClient) {
				replyMsg := &WorldReplyMsg{
					StatusCode: e.SUCCESS,
					Code:       e.WebsocketSuccess,
					Content:    "已连接至服务器",
				}

				// 序列化消息
				msg, err := json.Marshal(replyMsg)
				if err != nil {
					log.Printf("消息序列化失败: %v", err)
					return
				}

				// 在发送消息之前,检查conn.Socket是否为nil
				if conn.Socket == nil {
					log.Printf("Socket为空,无法发送消息: %v", conn.WorldConversations.Uid)
					return
				}

				// 发送消息给客户端
				err = conn.Socket.WriteMessage(websocket.TextMessage, msg)
				if err != nil {
					log.Printf("消息发送失败: %v", err)
					return
				}
				log.Printf("消息发送成功: %v", conn.WorldConversations.Uid)
			}(conn) // 将当前连接传递到 Goroutine

		case conn := <-WorldManager.Unregister: // 断开连接
			log.Printf("连接断开: %v", conn.WorldConversations.Uid)
			if conn == nil || conn.Socket == nil {
				log.Printf("无效的连接或socket为空")
				continue // 如果连接或socket为空,跳过这次循环
			}

			// 如果连接存在于客户端列表中,执行断开逻辑
			if _, ok := WorldManager.Client[utils.Strval(conn.WorldConversations.Uid)]; ok {
				go func(conn *WorldClient) {
					replyMsg := &WorldReplyMsg{
						Code:    e.WebsocketEnd,
						Content: "连接已断开",
					}

					// 序列化消息
					msg, err := json.Marshal(replyMsg)
					if err != nil {
						log.Printf("消息序列化失败: %v", err)
						return
					}

					// 在发送消息之前,检查conn.Socket是否为nil
					if conn.Socket == nil {
						log.Printf("Socket为空,无法发送断开消息: %v", conn.WorldConversations.Uid)
						return
					}

					// 发送消息给客户端
					err = conn.Socket.WriteMessage(websocket.TextMessage, msg)
					if err != nil {
						log.Printf("消息发送失败: %v", err)
						return
					}
					log.Printf("断开消息发送成功: %v", conn.WorldConversations.Uid)

					if conn.Send != nil {
						close(conn.Send)
					}
					delete(WorldManager.Client, utils.Strval(conn.WorldConversations.Uid))
				}(conn)
			}
		}
	}
}