phoenixframework / websock Goto Github PK
View Code? Open in Web Editor NEWA specification for Elixir apps to service WebSocket connections
License: MIT License
A specification for Elixir apps to service WebSocket connections
License: MIT License
Do you have any plans for adding a router to sock yet?
This is probably the last big obstacle to have Phoenix run on top of bandit.
I think it may be a good idea to add :close
atom value to allowed control opcodes, so that handle_in/2
and handle_info/2
could choose to push some data into the socket and close it immediately afterwards.
I ran into this issue while experimenting with moving a custom binary protocol websocket handler from Cowboy to Bandit with WebSock. Current implementation does some initial handshake-style message passing, and in case of failure it sends an in-protocol detailed error before closing the connection. I find it a valuable feature, especially for a binary protocol, since it allows giving the client some actionable context about a failed handshake.
I understand that I could do a send(self(), :do_close)
call and a {:push, {:binary, error_message}, %{state | to_be_closed: true}}
return, and add a special-case handler in handle_in/2
and handle_info/2
that would return a {:stop, :normal, state}
tuple if the to_be_closed
flag is set to true
, but I think that allowing :close
atom is a more appropriate solution for this problem. It seems to me that the concern of "push some data and close right after" is a generic one
Hey, Mat.
Quick question. I want to implement the ocpp protocol version 1.6j in Phoenix using WebSockets.
Now in specification, it is stated that:
Upon receiving the Charge Point’s request, the Central System has to finish the handshake with a
response as described in [RFC6455].
The following OCPP-J-specific conditions apply:
• If the Central System does not recognize the charge point identifier in the URL path, it SHOULD
send an HTTP response with status 404 and abort the WebSocket connection as described in
[RFC6455].
• If the Central System does not agree to using one of the subprotocols offered by the client, it MUST
complete the WebSocket handshake with a response without a Sec-WebSocket-Protocol header and
then immediately close the WebSocket connection.
So if the Central System accepts the above example request and agrees to using OCPP 1.6J with the
Charge Point, the Central System’s response will look as follows:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Protocol: ocpp1.6
The bold parts are found as such in every WebSocket handshake response, the other parts are specific
to this example.
The role of the Sec-WebSocket-Accept header is explained in [RFC6455].
The Sec-WebSocket-Protocol header indicates that the server will be using OCPP1.6J on this connection.
So basically I need to set in the reply a specific header, mainly "sec-websocket-protocol": "ocpp1.6"
Since Websock
Here is my implementation:
defmodule MyApp.WebSocketController do
alias MyApp.Websocket.Ocpp.ProtocolHandler
use MyApp, :controller
@timeout 60_000
def websocket(conn, params) do
conn
|> put_resp_header("sec-websocket-protocol", "ocpp1.6")
|> WebSockAdapter.upgrade(
ProtocolHandler,
params,
timeout: @timeout
)
end
end
Upon reviewing the response, this is what I get
query_params: %{},
query_string: "",
remote_ip: {127, 0, 0, 1},
req_cookies: %{},
req_headers: [
{"authorization", "Basic VEVTVENQMTpURVNUQ1Ax"},
{"connection", "Upgrade"},
{"host", "localhost:3333"},
{"sec-websocket-extensions", "permessage-deflate; client_max_window_bits"},
{"sec-websocket-key", "3F4psnvie6paAUxH5qzcUw=="},
{"sec-websocket-protocol", "ocpp1.6"},
{"sec-websocket-version", "13"},
{"upgrade", "websocket"}
],
request_path: "/ws/ocpp/1.6j/TESTCP1",
resp_body: nil,
resp_cookies: %{},
resp_headers: [
{"cache-control", "max-age=0, private, must-revalidate"},
{"x-request-id", "F31uOJucbjNfEDcAAAKF"},
{"referrer-policy", "strict-origin-when-cross-origin"},
{"x-content-type-options", "nosniff"},
{"x-download-options", "noopen"},
{"x-frame-options", "SAMEORIGIN"},
{"x-permitted-cross-domain-policies", "none"}
],
scheme: :http,
script_name: [],
secret_key_base: :...,
state: :unset,
status: nil
I wonder what am I missing. I believe there should be a better way of injecting the headers
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.