W3cubDocs

/Crystal 0.31

class HTTP::WebSocket

Defined in:

http/web_socket.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(host : String, path : String, port = nil, tls = false, headers = HTTP::Headers.new)Source

Opens a new websocket to the target host. This will also handle the handshake and will raise an exception if the handshake did not complete successfully.

require "http/web_socket"

HTTP::WebSocket.new("websocket.example.com", "/chat")            # Creates a new WebSocket to `websocket.example.com`
HTTP::WebSocket.new("websocket.example.com", "/chat", tls: true) # Creates a new WebSocket with TLS to `ẁebsocket.example.com`

def self.new(uri : URI | String, headers = HTTP::Headers.new)Source

Opens a new websocket using the information provided by the URI. This will also handle the handshake and will raise an exception if the handshake did not complete successfully. This method will also raise an exception if the URI is missing the host and/or the path.

Please note that the scheme will only be used to identify if TLS should be used or not. Therefore, schemes apart from wss and https will be treated as the default which is ws.

require "http/web_socket"

HTTP::WebSocket.new(URI.parse("ws://websocket.example.com/chat"))        # Creates a new WebSocket to `websocket.example.com`
HTTP::WebSocket.new(URI.parse("wss://websocket.example.com/chat"))       # Creates a new WebSocket with TLS to `websocket.example.com`
HTTP::WebSocket.new(URI.parse("http://websocket.example.com:8080/chat")) # Creates a new WebSocket to `websocket.example.com` on port `8080`
HTTP::WebSocket.new(URI.parse("ws://websocket.example.com/chat"),        # Creates a new WebSocket to `websocket.example.com` with an Authorization header
  HTTP::Headers{"Authorization" => "Bearer authtoken"})

Instance Method Detail

def close(message = nil)Source

def closed? : BoolSource

def on_binary(&on_binary : Bytes -> )Source

def on_close(&on_close : String -> )Source

def on_message(&on_message : String -> )Source

def on_ping(&on_ping : String -> )Source

def on_pong(&on_pong : String -> )Source

def ping(message = nil)Source

It's possible to send a PING frame, which the client must respond to with a PONG, or the server can send an unsolicited PONG frame which the client should not respond to.

See #pong.

def pong(message = nil)Source

Server can send an unsolicited PONG frame which the client should not respond to.

See #ping.

def runSource

def send(message)Source

def stream(binary = true, frame_size = 1024, &block)Source

© 2012–2019 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.31.1/HTTP/WebSocket.html