好的,所以我最近一直在使用 SocketIO4Net.Client,但遇到了问题。我在启动时调用 Hello 类中的 Go 函数,一旦调用它,我就会收到错误“使用http://127.0.0.1初始化握手时出错”
这是我使用 SocketIO 包连接到的 test.php 文件
<script src="https://api.icbit.se/socket.io/socket.io.js"></script>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
<script>
$(document).ready(function () {
var conn = io.connect('http://api-url');
conn.on('connect', function () {
alert('Connected');
conn.emit('message', { op: 'subscribe', channel: 'orderbook_BUM3' });
});
conn.on('message', function (data) {
console.log('Incoming message: ' + data.private);
});
});
</script>
这也是我使用的 csharp 代码
using SocketIOClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Fag_Ville
{
class Hello
{
public static Client broadcastSocketClient;
public static void Go()
{
broadcastSocketClient = new Client("http://127.0.0.1");
broadcastSocketClient.Opened += SocketOpened;
broadcastSocketClient.Message += SocketMessage;
broadcastSocketClient.SocketConnectionClosed += SocketConnectionClosed;
broadcastSocketClient.Error += SocketError;
while (!broadcastSocketClient.IsConnected)
{
broadcastSocketClient.Connect();
}
// register for 'connect' event with io server
broadcastSocketClient.On("connect", (cn) =>
{
var namespaceConnect = broadcastSocketClient.Connect("/namespacename");
// register for 'connect' event with io server
namespaceConnect.On("connect", (data) =>
{
namespaceConnect.Emit("test", "CONNECTED");
namespaceConnect.On("first", (message) =>
{
Console.WriteLine(message);
});
});
});
}
public static void SocketOpened(object sender, EventArgs e)
{
Console.WriteLine("SocketOpened\r\n");
Console.WriteLine("Connected to ICBIT API server!\r\n");
}
public static void SocketError(object sender, ErrorEventArgs e)
{
Console.WriteLine("socket client error:");
Console.WriteLine(e.Message);
}
public static void SocketConnectionClosed(object sender, EventArgs e)
{
Console.WriteLine("WebSocketConnection was terminated!");
}
public static void SocketMessage(object sender, MessageEventArgs e)
{
if (string.IsNullOrEmpty(e.Message.Event))
Console.WriteLine("Generic SocketMessage: {0}", e.Message.MessageText);
else
Console.WriteLine("Generic SocketMessage: {0} : {1}", e.Message.Event, e.Message.Json.ToJsonString());
}
public void Close()
{
if (broadcastSocketClient != null)
{
broadcastSocketClient.Opened -= SocketOpened;
broadcastSocketClient.Message -= SocketMessage;
broadcastSocketClient.SocketConnectionClosed -= SocketConnectionClosed;
broadcastSocketClient.Error -= SocketError;
broadcastSocketClient.Dispose(); // close & dispose of socket client
}
}
}
}