我正在努力在台式机上的UWP Application和Hololens 1之间建立消息通信。
根据Microsoft文档Websocket Implementation中Websockets的实现,我可以 使用socket.io Server实现对Nodejs的信令部分
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
var PORT = 3000;
var userId = 0;
http.listen(PORT, function () {
console.log('listening on *: ' + PORT);
});
io.on('connection', function(socket){
socket.userId = userId++;
console.log('a user connected, user id: ' + socket.userId);
socket.on('mess', function (msg) {
console.log('message from user#' + socket.userId + ": " + msg);
// Send message with emit"mess" to all client
io.emit('mess', msg);
});
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
两个客户端(uwp应用程序和hololens均可建立到服务器。 在计算机上,我可以向服务器发送一些数据。 我目前遇到的问题是Hololens无法从服务器收到消息。
对于通信部分,我使用包装器类,该类为2个客户端提供发送和接收功能。该包装器类已生成到dll,以便在2个差异项目(uolp应用和hololens的unity应用)中进行引用
// Declare "Using" part here...
namespace SocketWrapperNamespace
{
public delegate void OnReceiveMessengerCallback(string msg);
public delegate void OnDisconnectCallback(bool connect);
public class SocketWrapper
{
public event OnReceiveMessengerCallback OnReceiveMessenger;
public event OnDisconnectCallback onDisconnect;
#if !UNITY_EDITOR
private Uri _uri;
private MessageWebSocket messageWebSocket;
private DataWriter dataWriter;
#endif
// Client can call this function to connect
public void Connect(string uri)
{
#if !UNITY_EDITOR
try
{
Connector(uri);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
#endif
}
private async void Connector(string uri)
{
#if !UNITY_EDITOR
// Code here look pretty much like in the example from microsoft documentation
#endif
}
// Function for client to send message to server
public void SendMessage(string emiter, string message)
{
#if !UNITY_EDITOR
try
{
Emit(emiter, message);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
#endif
}
//Emitter
private async void Emit(string emiter, string message)
{
#if !UNITY_EDITOR
try
{
if (messageWebSocket == null)
{
return;
}
if (dataWriter == null)
{
return;
}
string form = string.Format("42[\"{0}\",\"{1}\"]", emiter, message);
dataWriter.WriteString(form);
await dataWriter.StoreAsync();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
#endif
}
//Disconnect
public void Disconnect()
{
#if !UNITY_EDITOR
// Disconnecting server ...
#endif
}
//Message Arrived Handler
private void WebSocket_MessageArrived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
{
#if !UNITY_EDITOR
try
{
if (OnReceiveMessenger == null)
{
return;
}
using (DataReader dataReader = args.GetDataReader())
{
if (dataReader != null)
{
dataReader.UnicodeEncoding = UnicodeEncoding.Utf8;
string message = dataReader.ReadString(dataReader.UnconsumedBufferLength);
OnReceiveMessenger?.Invoke(message);
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
#endif
}
//Websocket Close Handler
private void WebSocket_Closed(IWebSocket sender, WebSocketClosedEventArgs args)
{
#if !UNITY_EDITOR
// Close work here ...
#endif
}
}
在Hololens上,我准备与服务器的连接并收听收到的消息事件。
// hololens client call this method to initialize the connection
public void OnStart()
{
socketWrapper = new SocketWrapper();
uri = String.Concat("ws://",server,":3000/socket.io/?EIO=3&transport=websocket");
#if !UNITY_EDITOR
socketWrapper.Connect(uri);
socketWrapper.OnReceiveMessenger += OnMessageArrived_Handler;
socketWrapper.onDisconnect += OnDisconnect_Handler;
#endif
}
//Message Arrived Handler
private void OnMessageArrived_Handler(string msg)
{
// Here I do not received any message from server
}
我让她犯了什么错误?我想知道,即使我可以从hololens连接到服务器,但订阅部分似乎无法正常工作? 考虑这2个项目在不同的位置,并且dll发生相同的情况。
我认为我必须将SocketWrapper类设置为Singleton,因为hololens部件为此实例化了一个新的Object,因此在这里订阅可能不起作用
使用其他实现解决此问题的更好建议会有所帮助。
谢谢
答案 0 :(得分:1)
我认为现在在C#和Unity环境中使用WebSocket的最佳解决方案是WebSocketSharp library。 它具有出色的实现功能,并且至今已更新了6年。此外,它还有3个在不同情况下如何使用它的示例。