仅当我从原始聊天中发送消息时,Websocket消息才发送

时间:2018-12-17 04:51:07

标签: go websocket

我实际上有一个问题。因此,我实际上制作了一个使用不和谐的bot和websocket的程序。我想从网络聊天中发送消息到不和谐,并向网络聊天中发送不和谐的消息。现在,当我发送一条消息时,一切都发送完了,但不是我想要的。现在,仅当在网络聊天中编写消息时,才会显示从不和谐中收到的消息。我该如何解决?这是代码
main.go

package main

import (
    "fmt"

    "./bot"
    "./config"
    "./sockets"
)

func main() {
    err := config.ReadConfig()

    if err != nil {
        fmt.Println(err.Error())
        return
    }

    bot.Start()
    sockets.RunSockets()
    return
}

bot.go

package bot

import (
    "fmt"

    "../config"
    "github.com/bwmarrin/discordgo"
)

var (
    // BotID contains the ID of the bot
    BotID   string
    discord *discordgo.Session
    // Websockets contains all of data that are going to be sent to Server
    Websockets   WebsocketStruct
    socketsDatas WebData
)

// WebData contains the datas coming from Web
type WebData struct {
    Message  string
    Nickname string
}

// WebsocketStruct contains how the websocket will recieve the datas
type WebsocketStruct struct {
    DiscordMessage string
    DiscordUser    string
}

// Start the bot
func Start() {
    discord, err := discordgo.New("Bot " + config.Token)

    if err != nil {
        fmt.Println(err.Error())
        return
    }

    u, err := discord.User("@me")

    if err != nil {
        fmt.Println(err.Error())
    }

    discord.AddHandler(messageHandler)

    discord.ChannelMessageSend("516626040622284802", socketsDatas.Nickname+" said from chat: "+socketsDatas.Message)

    BotID = u.ID

    err = discord.Open()

    if err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Println("Bot is now running")
}

func messageHandler(session *discordgo.Session, msg *discordgo.MessageCreate) {
    if msg.Author.ID == BotID {
        return
    }

    Websockets.DiscordMessage = msg.Content
    Websockets.DiscordUser = msg.Author.Username
}

// RecieveData function is meant to recieve datas from the web
func RecieveData(datas WebData) {
    socketsDatas = datas
}

websockets.go

package sockets

import (
    "fmt"
    "net/http"
    "sync"

    "../bot"
    "golang.org/x/net/websocket"
)

// Data structure
type Data struct {
    Message  string `json:"message"`
    Nickname string `json:"nickname"`
}

var (
    count = 0
    hub   = make(map[int]*websocket.Conn)
    mutex = &sync.Mutex{}
    // Webdatas contains the struct that the bot needs to recieve messages from Web
    Webdatas bot.WebData
)

// RunSockets is made for running sockets
func RunSockets() {
    fmt.Println("Server started")
    http.Handle("/connws/", websocket.Handler(func(ws *websocket.Conn) {
        current := connexion(ws)
        fmt.Println("The hub:", hub)
        var data Data
        for {
            err := websocket.JSON.Receive(ws, &data)
            if err != nil {
                delete(hub, current)
                ws.Close()
                fmt.Println("The hub:", hub)
                return
            }
            for _, i := range hub {
                discordMessage := bot.Websockets
                if discordMessage.DiscordMessage != "" && discordMessage.DiscordUser != "" {
                    go websocket.Message.Send(i, discordMessage.DiscordUser+" said from Discord: "+discordMessage.DiscordMessage+"<br>")
                }
                _ = websocket.Message.Send(i, string(data.Nickname+" said: "+data.Message+"<br>"))
                Webdatas.Message = data.Message
                Webdatas.Nickname = data.Nickname
                bot.RecieveData(Webdatas)
            }
        }
    }))

    http.ListenAndServe(":47563", nil)
}

func connexion(ws *websocket.Conn) int {
    mutex.Lock()
    hub[count] = ws
    current := count
    count++
    mutex.Unlock()
    return current
}

感谢您的帮助:)

0 个答案:

没有答案