使用http://127.0.0.1初始化握手时出错

时间:2015-08-25 16:14:42

标签: c# php node.js socket.io socketio4net

好的,所以我最近一直在使用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
            }
        }
    }
}

0 个答案:

没有答案