Reading WebSocket Locations
View WebSockets ReferenceView the WebSockets Reference.
View TMM API Sample CodeSample code for TMM-API integrations is available on GitHub, including projects for Android, iOS, .NET MAUI (cross platform), and web applications.
TMM serves location data over WebSocket connections. TMM provides two versions of the WebSocket API: V1 and V2.
The process of reading location data from a WebSocket connection involves connecting to the appropriate WebSocket endpoint and handling incoming messages that contain location updates. Location data is in JSON format.
To read location data from a WebSocket connection, you need to establish a connection to the appropriate WebSocket endpoint and then listen for incoming messages that contain location updates. The following example demonstrates how to do this in C#.
internal async Task ReadPositionsAsync(CancellationToken cancel){ try { // Open an insecure connection to WebSocketV2. Uri uri = new Uri($"ws://localhost:9639"); using ClientWebSocket client = new ClientWebSocket(); await client.ConnectAsync(uri, cancel);
while (!cancel.IsCancellationRequested) { // Will continue to run as long as the WebSocket is connected, unless canceled.
using MemoryStream messageBuffer = new(); var buffer = new ArraySegment<byte>(new byte[1024]); WebSocketReceiveResult result; bool closed = false;
// A single message may arrive across multiple WebSocket frames. do { result = await client.ReceiveAsync(buffer, cancel);
if (result.MessageType == WebSocketMessageType.Close) { await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None); closed = true; break; }
if (result.Count > 0) { messageBuffer.Write(buffer.Array!, buffer.Offset, result.Count); } } while (!result.EndOfMessage);
if (closed) { break; }
if (result.MessageType == WebSocketMessageType.Text && messageBuffer.Length > 0) { // parse the location data. string jsonString = Encoding.UTF8.GetString(messageBuffer.ToArray()); JsonNode? jnode = JsonNode.Parse(jsonString); if (jnode is not null) { double? latitude = jnode["latitude"]?.GetValue<double>(); double? longitude = jnode["longitude"]?.GetValue<double>(); double? altitude = jnode["altitude"]?.GetValue<double>(); } } } } catch (TaskCanceledException) { }}