本文整理汇总了Golang中gopkg/in/igm/sockjs-go/v2/sockjs.Session类的典型用法代码示例。如果您正苦于以下问题:Golang Session类的具体用法?Golang Session怎么用?Golang Session使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Session类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: echoHandler
func echoHandler(session sockjs.Session) {
log.Println("new sockjs session established")
var closedSession = make(chan struct{})
chat.Publish("[info] new participant joined chat")
defer chat.Publish("[info] participant left chat")
go func() {
reader, _ := chat.SubChannel(nil)
for {
select {
case <-closedSession:
return
case msg := <-reader:
if err := session.Send(msg.(string)); err != nil {
return
}
}
}
}()
for {
if msg, err := session.Recv(); err == nil {
chat.Publish(msg)
continue
}
break
}
close(closedSession)
log.Println("sockjs session closed")
}
示例2: authenticate
func authenticate(s sockjs.Session, ctx *context.Context) (*store.Session, error) {
val, err := s.Recv()
if err != nil {
return nil, err
}
var req struct {
Type string
Token string
}
if err := json.Unmarshal([]byte(val), &req); err != nil {
return nil, err
}
if req.Type != typeConnect {
return nil, fmt.Errorf("hub: expected \"connect\" got \"%s\"", req.Type)
}
sess, err := auth.SessionFromToken(ctx, req.Token)
if err != nil {
return nil, err
}
if err := send(s, map[string]string{
"Type": typeConnect,
}); err != nil {
return nil, err
}
return sess, nil
}
示例3: send
func send(s sockjs.Session, data interface{}) error {
b, err := json.Marshal(data)
if err != nil {
return err
}
return s.Send(string(b))
}
示例4: Read
func (s *SockJS) Read(session sockjs.Session) {
for {
if raw, err := session.Recv(); err == nil {
s.processMessage(raw, session)
} else {
break
}
}
}
示例5: echoHandler
func echoHandler(session sockjs.Session) {
for {
if msg, err := session.Recv(); err == nil {
session.Send(msg)
continue
}
break
}
}
示例6: quoridorHandler
func quoridorHandler(session sockjs.Session) {
for {
if msg, err := session.Recv(); err == nil {
handleMsg(session, msg)
continue
}
break
}
}
示例7: respondError
func respondError(session sockjs.Session, message string) {
result := cmd.Response{
Status: "error",
Result: message,
}
response, err := json.Marshal(result)
if err != nil {
panic(err)
}
session.Send(string(response))
}
示例8: echoHandler
func echoHandler(session sockjs.Session) {
log.Println("new sockjs session established")
for {
if msg, err := session.Recv(); err == nil {
session.Send(msg)
continue
}
break
}
log.Println("sockjs session closed")
}
示例9: leave
func (h *hub) leave(s sockjs.Session, user *store.User) {
h.ch <- func() {
us := h.users[user.Id]
if us == nil {
return
}
delete(us.sess, s.ID())
log.Printf("exit: %s", user.Name)
}
}
示例10: wsHandler
func wsHandler(session sockjs.Session) {
log.Println("new sockjs session established")
go func() {
reader, _ := broadcaster.SubChannel(nil)
for {
status := <-reader
if err := session.Send(status.(string)); err != nil {
return
}
}
}()
}
示例11: echoHandler
func echoHandler(conn sockjs.Session) {
log.Println("New connection created")
for {
if msg, err := conn.Recv(); err != nil {
break
} else {
if err := conn.Send(msg); err != nil {
break
}
}
}
log.Println("Sessionection closed")
}
示例12: AddClient
func (this *Room) AddClient(client sockjs.Session) {
if this.Len() >= this.max {
return
} else if indexOf(this.clients, client) == -1 {
message := "{\"type\":\"userConnected\"}"
this.sendToAll(message)
for i := 0; i < this.Len(); i++ {
client.Send(message)
}
this.clients = append(this.clients, client)
}
}
示例13: enter
func (h *hub) enter(s sockjs.Session, user *store.User) {
h.ch <- func() {
us := h.users[user.Id]
if us == nil {
us = &userSession{
user: user,
sess: map[string]sockjs.Session{},
}
h.users[user.Id] = us
}
us.sess[s.ID()] = s
log.Printf("enter: %s", user.Name)
}
}
示例14: Register
func Register(session sockjs.Session, oldId string, name string) error {
if player, ok := FindPlayer(oldId); ok {
player.Update(session, session.ID(), name)
fmt.Println("Find an existed player & update it")
return nil
} else {
if p := FindPlayerByName(name); p != nil {
return NewError("The name is already registered!")
}
id := session.ID()
players[id] = &GamePlayer{id, name, 0, &session, nil, nil, nil, false}
fmt.Println("Register as new")
return nil
}
}
示例15: Handler
func (ws *Ws) Handler(session sockjs.Session) {
for {
if msg, err := session.Recv(); err == nil {
log.Printf("ws: received: %v", msg)
message := map[string]interface{}{}
err = json.Unmarshal([]byte(msg), &message)
if err != nil {
respondError(session, err.Error())
continue
}
command, ok := message["cmd"]
if !ok {
respondError(session, "`cmd` field must be specified!")
continue
}
commandName, ok := command.(string)
if !ok {
respondError(session, "`cmd` must be a string")
continue
}
params, ok := message["params"]
if !ok {
respondError(session, "`params` field must be specified!")
continue
}
paramsMap, ok := params.(map[string]interface{})
if !ok {
respondError(session, "`params` must be an object!")
continue
}
result, err := ws.Run(commandName, paramsMap)
if err != nil {
respondError(session, err.Error())
continue
}
response, err := json.Marshal(result)
if err != nil {
panic(err)
}
session.Send(string(response))
} else {
log.Println(err)
break
}
}
}