Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket: updates the websocket server to be able to use another server socket #20103

Merged
merged 5 commits into from Dec 19, 2023

Conversation

el-gringo
Copy link
Contributor

@el-gringo el-gringo commented Dec 6, 2023

Hello, I am very new to vlang, so I might have done things wrong.

I updated the websocket.Server to allow it to not start a listener socket in order to use an external one (ie: vweb).

There are some caveats :

  • the vweb request handler never ends. I would like to use middlewares, but I am not sure how to access the WebSocket server then
  • in the example, I had to push force a .js file. This is a small script and could be inlined in a <script type="text/javascript"> in index.html but the compiler try to parse it and fail.

🤖[deprecated] Generated by Copilot at 8174157

This pull request adds an example of using the net.websocket module to create a headless WebSocket server and a vweb application that serves a simple WebSocket client. It also adds support for the Sec-WebSocket-Key header in the net.http module, which is needed for WebSocket handshaking. The example demonstrates how to handle WebSocket events, send and receive messages, and update the HTML page using JavaScript.

🤖[deprecated] Generated by Copilot at 8174157

  • Add a vweb_websocket.v file that creates a net.websocket.Server instance and registers callbacks for WebSocket events (link)
  • Define a route for the WebSocket endpoint and pass the connection and the key to the handle_handshake method of the WebSocket server (link, link)
  • Add a new enum value and cases for the Sec-WebSocket-Key header in the net.http.Header enum and its methods (link, link, link)
  • Add a new field is_headless to the net.websocket.Server struct to indicate whether the server should start its own listener or use an external server (link)
  • Refactor the accept_client method of the net.websocket.Server struct to extract the logic for attaching a client and sending the handshake response to a new method attach_client (link)
  • Add an HTML file and a JavaScript file that demonstrate the WebSocket client functionality (link, link)
    • Add an index.html file that loads the websocket_client.js script and displays an ordered list of messages exchanged with the server (link)
    • Add a websocket_client.js file that implements a simple WebSocket client that connects to the server, sends and receives messages, and updates the HTML page with the message list (link)

@spytheman
Copy link
Member

I think this draft PR is good to be merged, does it need something else @el-gringo ?

@spytheman spytheman marked this pull request as ready for review December 17, 2023 18:50
christophe added 5 commits December 18, 2023 15:40
…er server to handle handshake resquests

- adds `is_headless` attribute to `webscokect.Server`
- adds public function to handle handcheck from a `TcpConn` for a given key
- adds a `vweb` example
- adds `Sec-WebSocket-Key` as common HTTP header
@spytheman
Copy link
Member

(rebased over current master)

@spytheman spytheman changed the title websocket: updates the websocket server to use an other server socket websocket: updates the websocket server to be able to use another server socket Dec 19, 2023
Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman spytheman merged commit 5be5cd9 into vlang:master Dec 19, 2023
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants