-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat2.go
71 lines (60 loc) · 1.55 KB
/
chat2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
66
67
68
package main
import "fmt"
import "redis"
import "bytes"
import "os"
func main() {
var input string
fmt.Print("What game server chat u want in on: ")
fmt.Scanln(&input)
var channelKey string = "games:"+input+":chat"
msg := connectClient(channelKey)
go spamChat1(channelKey)
chatbuf := bytes.NewBuffer(<-msg)
chatbuf.WriteTo(os.Stdout)
fmt.Scanln(&input)
}
func connectClient(chanKey string) (redis.PubSubChannel) {
spec := redis.DefaultSpec().Password("go-redis")
client, e := redis.NewPubSubClientWithSpec(spec)
if e != nil {
fmt.Print("Error creating client for: ", e)
}
defer client.Quit()
fmt.Println("before subbing")
client.Subscribe("chat")
fmt.Println("after subbing")
return client.Messages("chat")
//fmt.Println(string(<-client.Messages("chat")))
//fmt.Println(string(<-client.Messages("chat")))
//for {
// chatbuf := bytes.NewBuffer(<-client.Messages("chat"))
// chatbuf.WriteTo(os.Stdout)
// }
}
func spamChat1(chanKey string) {
fmt.Println("spamchat being called")
spec := redis.DefaultSpec().Password("go-redis")
client, e := redis.NewAsynchClientWithSpec(spec)
if e != nil {
fmt.Print("Error creating spam client for: ", e)
}
defer client.Quit()
for {
//var fr redis.FutureInt64
//var fr2 redis.FutureBool
var buf bytes.Buffer
buf.Write([]byte("hello"))
bt := buf.Bytes()
_, e = client.Publish("chat", bt)
if e != nil {
fmt.Println("error in publishing: ", e)
}
_, e = client.Rpush("chatlog",bt)
if e != nil {
fmt.Println("error in storing list: ", e)
}
//fr.Get()
//fr2.Get()
}
}