The C# .NET SDK currently only supports legacy versions of Nitric prior to v1. This version is maintained for compatibility with existing projects and not recommended for new projects. New projects should be started using a supported SDK (presented automatically using the `nitric new` command) orget in touch to request an update to the latest version.
.NET - Websocket.Send()
Send a message from the websocket to a connection.
using Nitric.Sdk;
var websocket = Nitric.Websocket("public");
websocket.Connection("D28BA458-BFF4-404A").Send("Hello World");
Nitric.Run();
Parameters
- Name
message
- Required
- Required
- Type
- string
- Description
The message that should be sent to the connection.
Examples
Broadcasting a message to all connections.
Do not send messages to a connection during it's connect
callback, if you
need to acknowledge connection, do so by using a topic
using System;
using Nitric.Sdk.Function;
using Nitric.Sdk.Resource;
class OpenConnection
{
public string Id { get; set; }
}
var websocket = Nitric.Websocket("public");
var collections = Nitric.Collection<OpenConnection>("connections").With(
CollectionPermission.Writing, CollectionPermission.Reading, CollectionPermission.Deleting
);
websocket.On(WebsocketEventType.Connected, ctx =>
{
collections.Doc(ctx.Req.ConnectionId).Set(new OpenConnection { Id = ctx.Req.ConnectionId });
return ctx;
});
websocket.On(WebsocketEventType.Disconnected, ctx =>
{
collections.Doc(ctx.Req.ConnectionId).Delete();
return ctx;
});
websocket.On(WebsocketEventType.Message, ctx =>
{
var connections = collections.Query().Fetch();
connections.Documents.ForEach(connection => {
var message = ctx.Req.Message();
websocket.Connection(connection.Content.Id).Send(message);
});
return ctx;
});
Nitric.Run();