Skip to content

Commit d5da246

Browse files
feat(PubSubApi): publish a stream message
1 parent 2973066 commit d5da246

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/CoreApi/PubSubApi.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ internal PubSubApi(IpfsClient ipfs)
5656

5757
public Task PublishAsync(string topic, Stream message, CancellationToken cancel = default(CancellationToken))
5858
{
59-
throw new NotImplementedException();
59+
using (MemoryStream ms = new MemoryStream())
60+
{
61+
message.CopyTo(ms);
62+
return PublishAsync(topic, ms.ToArray(), cancel);
63+
}
6064
}
6165

6266
public async Task PublishAsync(string topic, string message, CancellationToken cancel = default(CancellationToken))

test/CoreApi/PubSubApiTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Newtonsoft.Json.Linq;
44
using System;
55
using System.Collections.Generic;
6+
using System.IO;
67
using System.Linq;
78
using System.Text;
89
using System.Threading;
@@ -200,5 +201,32 @@ await ipfs.PubSub.SubscribeAsync(topic, msg =>
200201
cs.Cancel();
201202
}
202203
}
204+
205+
[TestMethod]
206+
public async Task Subscribe_StreamMessage()
207+
{
208+
var messages = new List<IPublishedMessage>();
209+
var expected = new byte[] { 0, 1, 2, 4, (byte)'a', (byte)'b', 0xfe, 0xff };
210+
var ipfs = TestFixture.Ipfs;
211+
var topic = "net-ipfs-http-client-test-" + Guid.NewGuid().ToString();
212+
var cs = new CancellationTokenSource();
213+
try
214+
{
215+
await ipfs.PubSub.SubscribeAsync(topic, msg =>
216+
{
217+
messages.Add(msg);
218+
}, cs.Token);
219+
var ms = new MemoryStream(expected, false);
220+
await ipfs.PubSub.PublishAsync(topic, ms);
221+
222+
await Task.Delay(1000);
223+
Assert.AreEqual(1, messages.Count);
224+
CollectionAssert.AreEqual(expected, messages[0].DataBytes);
225+
}
226+
finally
227+
{
228+
cs.Cancel();
229+
}
230+
}
203231
}
204232
}

0 commit comments

Comments
 (0)