---
title: "cancel(with:reason:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/urlsessionwebsockettask/cancel(with:reason:)"
---

# cancel(with:reason:)

Sends a close frame with the given close code and optional close reason.

## Declaration

```swift
func cancel(with closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?)
```

## Parameters

- `closeCode`: A doc://com.apple.foundation/documentation/Foundation/URLSessionWebSocketTask/CloseCode-swift.enum that indicates the reason for closing the connection.
- `reason`: Optional further information to explain the closing. The value of this parameter is defined by the endpoints, not by the standard.

## Discussion

Discussion If you call cancel() on the task instead of this method, it sends a cancellation frame with no close code or reason.

## See Also

### Closing the connection

- [closeCode](foundation/urlsessionwebsockettask/closecode-swift.property.md)
- [URLSessionWebSocketTask.CloseCode](foundation/urlsessionwebsockettask/closecode-swift.enum.md)
- [closeReason](foundation/urlsessionwebsockettask/closereason.md)
