Giter Club home page Giter Club logo

Comments (8)

Allenxuxu avatar Allenxuxu commented on May 25, 2024

这是一个特性需求,我把 issue 移到这里。

目前 websocket 插件支持还不够完善。
不过插件和 gev 框架是分开的,修改 websocket 相关部分就好了。
通过 connection 的 Context 保存 header ,似乎改动不大。

func (c *Connection) SetContext(ctx interface{}) {
	c.ctx = ctx
}

https://github.com/Allenxuxu/gev/tree/master/plugins/websocket

from gev.

lyfunny avatar lyfunny commented on May 25, 2024

这个EventLoop中的onMessage执行是同步的,如果里面有网络调用,会阻塞EventLoop,生成一个gorutine执行的话返回消息有发送不了了,websocket Send没法用

from gev.

MrChang0 avatar MrChang0 commented on May 25, 2024

这个EventLoop中的onMessage执行是同步的,如果里面有网络调用,会阻塞EventLoop,生成一个gorutine执行的话返回消息有发送不了了,websocket Send没法用

网络调用为什么会阻塞EventLoop?

from gev.

lyfunny avatar lyfunny commented on May 25, 2024

这个EventLoop中的onMessage执行是同步的,如果里面有网络调用,会阻塞EventLoop,生成一个gorutine执行的话返回消息有发送不了了,websocket Send没法用

网络调用为什么会阻塞EventLoop?

我的cpu32核心,eventloop是由32个goroutine执行的,当执行到OnMessage的时候,如果发生网络调用,那么这个goroutine就休眠了,那么就没有goroutine执行eventloop了,没法处理到来的请求了.现在我是在OnMessage里面新生成一个goroutine执行业务的,缺点就是返回数据需要用Send调用,OnMessage的返回值没用

from gev.

Allenxuxu avatar Allenxuxu commented on May 25, 2024

这个EventLoop中的onMessage执行是同步的,如果里面有网络调用,会阻塞EventLoop,生成一个gorutine执行的话返回消息有发送不了了,websocket Send没法用

网络调用为什么会阻塞EventLoop?

我的cpu32核心,eventloop是由32个goroutine执行的,当执行到OnMessage的时候,如果发生网络调用,那么这个goroutine就休眠了,那么就没有goroutine执行eventloop了,没法处理到来的请求了.现在我是在OnMessage里面新生成一个goroutine执行业务的,缺点就是返回数据需要用Send调用,OnMessage的返回值没用

不应该在 OnMessage 做耗时的操作,如果比较耗时,最好只在 OnMessage 里切割数据,然后使用 Send 发送。

from gev.

Allenxuxu avatar Allenxuxu commented on May 25, 2024

@lyfunny 考虑 在 gev 里增加一个协程池, OnMessage 函数放到协程池中执行。

from gev.

Allenxuxu avatar Allenxuxu commented on May 25, 2024

https://github.com/Allenxuxu/gev/blob/master/example/websocket/main.go

websocke 增加了取 header 的例子 @lyfunny

func main() {
//....

	wsUpgrader := &ws.Upgrader{}
	wsUpgrader.OnHeader = func(c *connection.Connection,key, value []byte) error {
		fmt.Println(string(key),":" ,string(value))

              // 这个回调函数会在 协议升级成功被调用
              // 在这里可以将需要的 header 保存起来,通过 c.SetContext 方法

		return nil
	}

	s, err := NewWebSocketServer(handler, wsUpgrader,
		gev.Network("tcp"),
		gev.Address(":"+strconv.Itoa(port)),
		gev.NumLoops(loops))
	if err != nil {
		panic(err)
	}

	s.Start()
}

from gev.

yiippee avatar yiippee commented on May 25, 2024

https://github.com/Allenxuxu/gev/blob/master/example/websocket/main.go

websocke 增加了取 header 的例子 @lyfunny

func main() {
//....

	wsUpgrader := &ws.Upgrader{}
	wsUpgrader.OnHeader = func(c *connection.Connection,key, value []byte) error {
		fmt.Println(string(key),":" ,string(value))

              // 这个回调函数会在 协议升级成功被调用
              // 在这里可以将需要的 header 保存起来,通过 c.SetContext 方法

		return nil
	}

	s, err := NewWebSocketServer(handler, wsUpgrader,
		gev.Network("tcp"),
		gev.Address(":"+strconv.Itoa(port)),
		gev.NumLoops(loops))
	if err != nil {
		panic(err)
	}

	s.Start()
}

这样设置好像没用啊,你在这里设置为true了

		out, _, err = p.upgrade.Upgrade(c, buffer)
		if err != nil {
			log.Error("Websocket Upgrade :", err)
			return
		}
		c.SetContext(true)        // 设置为true,标志已经upgrade过了

from gev.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.