• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang sockjs.Session类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中gopkg/in/igm/sockjs-go/v2/sockjs.Session的典型用法代码示例。如果您正苦于以下问题:Golang Session类的具体用法?Golang Session怎么用?Golang Session使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Session类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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")
}
开发者ID:wavded,项目名称:sockjs-go,代码行数:29,代码来源:webchat.go


示例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
}
开发者ID:kellegous,项目名称:404,代码行数:32,代码来源:hub.go


示例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))
}
开发者ID:kellegous,项目名称:404,代码行数:8,代码来源:hub.go


示例4: Read

func (s *SockJS) Read(session sockjs.Session) {
	for {
		if raw, err := session.Recv(); err == nil {
			s.processMessage(raw, session)
		} else {
			break
		}
	}
}
开发者ID:silentnull,项目名称:dockership,代码行数:9,代码来源:sockjs.go


示例5: echoHandler

func echoHandler(session sockjs.Session) {
	for {
		if msg, err := session.Recv(); err == nil {
			session.Send(msg)
			continue
		}
		break
	}
}
开发者ID:dnamenon,项目名称:webshop,代码行数:9,代码来源:routes.go


示例6: quoridorHandler

func quoridorHandler(session sockjs.Session) {
	for {
		if msg, err := session.Recv(); err == nil {
			handleMsg(session, msg)
			continue
		}
		break
	}
}
开发者ID:KingBright,项目名称:grouplay,代码行数:9,代码来源:handler.go


示例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))
}
开发者ID:matobet,项目名称:verdi,代码行数:11,代码来源:websocket.go


示例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")
}
开发者ID:wavded,项目名称:sockjs-go,代码行数:11,代码来源:webecho.go


示例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)
	}
}
开发者ID:kellegous,项目名称:404,代码行数:11,代码来源:hub.go


示例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
			}
		}
	}()
}
开发者ID:nh0815,项目名称:golem,代码行数:12,代码来源:main.go


示例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")
}
开发者ID:eelcocramer,项目名称:sockjs-go,代码行数:13,代码来源:server.go


示例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)
	}
}
开发者ID:nickalie,项目名称:GoSockJSExample,代码行数:14,代码来源:room.go


示例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)
	}
}
开发者ID:kellegous,项目名称:404,代码行数:14,代码来源:hub.go


示例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
	}
}
开发者ID:KingBright,项目名称:grouplay,代码行数:15,代码来源:player_manager.go


示例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
		}
	}
}
开发者ID:matobet,项目名称:verdi,代码行数:46,代码来源:websocket.go


示例16: sockjsHandler

func (k *Kite) sockjsHandler(session sockjs.Session) {
	defer session.Close(0, "")

	// This Client also handles the connected client.
	// Since both sides can send/receive messages the client code is reused here.
	c := k.NewClient("")
	c.session = session

	go c.sendHub()
	c.wg.Add(1) // with sendHub we added a new listener

	k.callOnConnectHandlers(c)

	// Run after methods are registered and delegate is set
	c.readLoop()

	c.callOnDisconnectHandlers()
	k.callOnDisconnectHandlers(c)
}
开发者ID:gotao,项目名称:kite,代码行数:19,代码来源:kite.go


示例17: sockjsHandler

func sockjsHandler(client sockjs.Session) {
	//new client connected
	for {
		if msg, err := client.Recv(); err == nil {
			//message received
			if strings.HasPrefix(msg, "join") {

				room, ok := clientToRooms[client]

				if ok {
					removeClientFromRoom(room, client)
				}

				roomId, err := strconv.Atoi(strings.TrimPrefix(msg, "join"))

				if err != nil {
					continue
				}

				room, ok = rooms[roomId]

				//create new room if not exists
				if !ok {
					room = ng.NewRoom(roomId, 2)
					rooms[roomId] = room
				}

				clientToRooms[client] = room
				room.AddClient(client)

			} else if room, ok := clientToRooms[client]; ok {
				room.Send(msg, client)
			}
		} else {
			break
		}
	}

	if room, ok := clientToRooms[client]; ok {
		removeClientFromRoom(room, client)
	}
}
开发者ID:nickalie,项目名称:GoSockJSExample,代码行数:42,代码来源:main.go


示例18: receiveMessage

func receiveMessage(session sockjs.Session) {
	reader, _ := pub.SubChannel(nil)
	for {
		select {
		case msg, ok := <-reader:
			if !ok {
				log.Println("channel closed")
				return
			}
			msg = msg.(*socketMessage)
			if body, err := json.Marshal(msg); err == nil {
				log.Println("message:", string(body))
				if err = session.Send(string(body)); err != nil {
					log.Println(err)
					return
				}
			}
		}
	}
}
开发者ID:sandbreaker,项目名称:photoshare,代码行数:20,代码来源:messages.go


示例19: echoHandler

func echoHandler(session sockjs.Session) {
	log.Println("new sockjs session established")
	var closedSession = make(chan struct{})
	session.Send(session.ID())
	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 {
			data := &WSDataSchema{}
			buf := bytes.NewBufferString(msg)
			err = json.Unmarshal(buf.Bytes(), data)
			if err == nil {
				_type := data.Type
				_endpoint := data.Endpoint
				_id := data.Id
				if _type == "container" {
					session.Send(_endpoint + _id)
					break
				}
			}
			chat.Publish(msg)
			//session.Send(msg)
			continue
		}
		break
	}
	close(closedSession)
	log.Println("sockjs session closed")
}
开发者ID:nkwangleiGIT,项目名称:dockerboard,代码行数:41,代码来源:ws.go


示例20: sockJSHandler

// sockJSHandler called when new client connection comes to SockJS endpoint.
func (app *Application) sockJSHandler(s sockjs.Session) {

	c, err := newClient(app, s)
	if err != nil {
		logger.ERROR.Println(err)
		return
	}
	defer c.clean()
	logger.INFO.Printf("new SockJS session established with uid %s\n", c.uid())

	for {
		if msg, err := s.Recv(); err == nil {
			err = c.message([]byte(msg))
			if err != nil {
				logger.ERROR.Println(err)
				s.Close(CloseStatus, "error handling message")
				break
			}
			continue
		}
		break
	}
}
开发者ID:rohan1790,项目名称:centrifugo,代码行数:24,代码来源:handlers.go



注:本文中的gopkg/in/igm/sockjs-go/v2/sockjs.Session类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang v5.SimpleMessage类代码示例发布时间:2022-05-28
下一篇:
Golang context.New函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap