Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions examples/experimental/websockets/test-echo.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { WebSocket } from "k6/experimental/websockets"

const CLOSED_STATE = 3
import { WebSocket, EventName, ReadyState, BinaryType } from "k6/experimental/websockets"

export default function() {
// local echo server should be launched with `make ws-echo-server-run`
var url = "wss://echo.websocket.org/"
var params = { "tags": { "my_tag": "hello" } };

let ws = new WebSocket(url, null, params)
ws.binaryType = "arraybuffer";

ws.addEventListener(EventName.Open, () => {
console.log('Connected');
})

ws.binaryType = BinaryType.ArrayBuffer;
ws.onopen = () => {
console.log('connected')
ws.send(Date.now().toString())
Expand All @@ -20,11 +23,11 @@ export default function() {
}, 1000);

let timeout1id = setTimeout(function() {
console.log('2 seconds passed, closing the socket')
console.log('3 seconds passed, closing the socket')
clearInterval(intervalId)
ws.close()

}, 2000);
}, 3000);

ws.onclose = () => {
clearTimeout(timeout1id);
Expand All @@ -42,7 +45,7 @@ export default function() {
}

// Multiple event handlers on the same event
ws.addEventListener("pong", () => {
ws.addEventListener(EventName.Pong, () => {
console.log("OTHER PONG!")
})

Expand All @@ -57,7 +60,7 @@ export default function() {
console.log(`Roundtrip time: ${Date.now() - parsed} ms`);

let timeoutId = setTimeout(function() {
if (ws.readyState == CLOSED_STATE) {
if (ws.readyState == ReadyState.Closed) {
console.log("Socket closed, not sending anything");

clearTimeout(timeoutId);
Expand Down
118 changes: 118 additions & 0 deletions internal/js/modules/k6/experimental/websockets/enums/enums.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Package enums is responsible for defining the enums available in the websocket module
package enums

// ReadyState is websocket specification's readystate
type ReadyState uint8

// Events represent the events that can be sent to the client
// https://dom.spec.whatwg.org/#event
const (
// EventOpen is the event name for the open event
EventOpen = "open"
// EventClose is the event name for the close event
EventClose = "close"
// EventMessage is the event name for the message event
EventMessage = "message"
// EventError is the event name for the error event
EventError = "error"
// EventPing is the event name for the ping event
EventPing = "ping"
// EventPong is the event name for the pong event
EventPong = "pong"
)

// ReadyState describes the possible states of a WebSocket connection.
const (
// StateConnecting is the state while the web socket is connecting
StateConnecting ReadyState = iota
// StateOpen is the state after the websocket is established and before it starts closing
StateOpen
// StateClosing is while the websocket is closing but is *not* closed yet
StateClosing
// StateClosed is when the websocket is finally closed
StateClosed
)

// BinaryType describes the possible types of binary data that can be
// transmitted over a Websocket connection.
const (
BinaryBlob = "blob"
BinaryArrayBuffer = "arraybuffer"
)

// WebSocket message types, as defined in RFC 6455, section 11.8.
const (
// The message is a text message. The text message payload is
// interpreted as UTF-8 encodedtext data.
MessageText = 1

// The message is a binary message.
MessageBinary = 2

// The message is a close control message. The optional message
// payload contains a numeric code and a text reason.
MessageClose = 8

// The message is a ping control message. The optional message
// payload is UTF-8 encoded text.
MessagePing = 9

// The message is a pong control message. The optional message
// payload is UTF-8 encoded text.
MessagePong = 10
)

// CompressionAlgorithm describes the possible compression algorithms.
const (
// Deflate compression algorithm.
// k6 supports only this compression algorithm.
AlgorithmDeflate = "deflate"
)

// GetEventsName maps field names to enum value
func GetEventsName() map[string]any {
return map[string]any{
"Open": EventOpen,
"Close": EventClose,
"Error": EventError,
"Message": EventMessage,
"Ping": EventPing,
"Pong": EventPong,
}
}

// GetReadyState maps field names to enum value
func GetReadyState() map[string]any {
return map[string]any{
"Connecting": StateConnecting,
"Open": StateOpen,
"Closing": StateClosing,
"Closed": StateClosed,
}
}

// GetBinaryType maps field names to enum value
func GetBinaryType() map[string]any {
return map[string]any{
"Blob": BinaryBlob,
"ArrayBuffer": BinaryArrayBuffer,
}
}

// GetMessageType maps field names to enum value
func GetMessageType() map[string]any {
return map[string]any{
"Text": MessageText,
"Binary": MessageBinary,
"Close": MessageClose,
"PingMessage": MessagePing,
"PongMessage": MessagePong,
}
}

// GetCompressionAlgorithm maps field names to enum value
func GetCompressionAlgorithm() map[string]any {
return map[string]any{
"Deflate": AlgorithmDeflate,
}
}
18 changes: 0 additions & 18 deletions internal/js/modules/k6/experimental/websockets/events/events.go

This file was deleted.

26 changes: 13 additions & 13 deletions internal/js/modules/k6/experimental/websockets/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/grafana/sobek"
"go.k6.io/k6/internal/js/modules/k6/experimental/websockets/events"
"go.k6.io/k6/internal/js/modules/k6/experimental/websockets/enums"
)

// eventListeners keeps track of the eventListeners for each event type
Expand All @@ -19,12 +19,12 @@ type eventListeners struct {

func newEventListeners() *eventListeners {
return &eventListeners{
open: newListener(events.OPEN),
message: newListener(events.MESSAGE),
error: newListener(events.ERROR),
close: newListener(events.CLOSE),
ping: newListener(events.PING),
pong: newListener(events.PONG),
open: newListener(enums.EventOpen),
message: newListener(enums.EventMessage),
error: newListener(enums.EventError),
close: newListener(enums.EventClose),
ping: newListener(enums.EventPing),
pong: newListener(enums.EventPong),
}
}

Expand Down Expand Up @@ -74,17 +74,17 @@ func (l *eventListener) all() []func(sobek.Value) (sobek.Value, error) {
// getTypes return event listener of a certain type
func (l *eventListeners) getType(t string) *eventListener {
switch t {
case events.OPEN:
case enums.EventOpen:
return l.open
case events.MESSAGE:
case enums.EventMessage:
return l.message
case events.ERROR:
case enums.EventError:
return l.error
case events.CLOSE:
case enums.EventClose:
return l.close
case events.PING:
case enums.EventPing:
return l.ping
case events.PONG:
case enums.EventPong:
return l.pong
default:
return nil
Expand Down
Loading