我正在尝试创建一个可以接受安全SSL和不安全的纯文本连接的服务器(为了向后兼容).我的代码几乎正常工作,除了从不安全客户端收到的第一个传输数据丢失了服务器上的前5个字节(字符).更具体地说,如果我在不安全的连接上传输30个字节,当服务器到达该OnClientDataReceived()函数时,则行" int iRx = nwStream.EndRead(asyn);",然后iRx = 25.从客户端发送的任何后续消息都包含所有发送的字节/字符.我怀疑连接的初始假设SSLStream可能是剥离前5个字节然后当它失败时,那5个字节已经从缓冲区中提取出来并且不再可用.有没有人知道我可以采用另一种方法来编写代码,以便服务器能够自动切换?
我试图避免做以下事情:
NetworkStream,然后请求升级到SSL流TcpListeners在两个不同的端口上设置两个(一个用于安全,一个用于不安全)/// Each client that connects gets an instance of the ConnectedClient class.
Class Pseudo_ConnectedClient
{
//Properties
byte[] Buffer; //Holds temporary buffer of read bytes from BeginRead()
TcpClient TCPClient; //Reference to the connected client
Socket ClientSocket; //The outer Socket Reference of the connected client
StringBuilder CurrentMessage; //concatenated chunks of data in buffer until we have a complete message (ends with …Run Code Online (Sandbox Code Playgroud) My goal for this app is to create logic that monitors the database and will trigger actions when a document is added to the database (like sending an email). However, since this application may not be started when a database is first populated, how can I manually create a ResumeToken that points to the very first document that was added into a collection, so that on the first run, I can start at the beginning and iterate through the changes …
运行新的MongDB服务器版本3.6,并尝试将更改流监视添加到集合以获取新插入和文档更新的通知时,我只接收更新通知,而不是插入.
这是我尝试添加手表的默认方式:
IMongoDatabase mongoDatabase = mongoClient.GetDatabase("Sandbox");
IMongoCollection<BsonDocument> collection = mongoDatabase.GetCollection<BsonDocument>("TestCollection");
var changeStream = collection.Watch().ToEnumerable().GetEnumerator();
changeStream.MoveNext();
var next = changeStream.Current;
Run Code Online (Sandbox Code Playgroud)
然后我从MongoDB下载了C#源代码,看看他们是如何做到这一点的.查看他们的更改流手表测试代码,他们创建一个新文档(插入),然后立即更改该文档(更新),然后设置Change Stream手表以接收'更新'通知.没有给出关于如何观察"插入"通知的示例.
我已经查看了MongoDB网站和SO上的Java和NodeJS示例,这些示例似乎很简单,并定义了一种查看插入和更新的方法:
var changeStream = collection.watch({ '$match': { $or: [ { 'operationType': 'insert' }, { 'operationType': 'update' } ] } });
Run Code Online (Sandbox Code Playgroud)
C#驱动程序的API有很大的不同,我原以为他们会为C#和Java和NodeJS保留相同的API.我发现C#没有或很少有例子可以做同样的事情.
我最接近的是以下尝试,但仍然失败,C#版本的文档非常有限(或者我找不到合适的位置).设置如下:
String json = "{ '$match': { 'operationType': { '$in': ['insert', 'update'] } } }";
var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };
PipelineDefinition<ChangeStreamDocument<BsonDocument>, ChangeStreamDocument<BsonDocument>> pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>().Match(Builders<ChangeStreamDocument<BsonDocument>>.Filter.Text(json,"json"));
Run Code Online (Sandbox Code Playgroud)
然后运行以下语句会抛出异常:
{"命令聚合失败:$ match with $ text仅允许作为第一个管道阶段."}
没有其他过滤器选项也有效,我还没有找到一种方法只需输入JSON作为字符串来设置'operationType'. …