0

只是想知道是否有人可以帮助我解决这个问题,下面是我的 Web 套接字 Helloserver 代码和 client.htm 代码。启动 Helloserver 后,我们运行客户端并注意到它没有连接到服务器,如果有人可以提供帮助,我们将不胜感激。

这是client.htm:

<!doctype html>
<html>
<head>
    <title>Web Sockets: Connecting to the Server</title>
    <link rel="Stylesheet" href="/global.css" type="text/css" />
</head>
<body>
    <div id="container">
        <h1>Connecting to Web Socket Server</h1>
        <ul id="log"></ul>
    </div>
</body>
<script src="../scripts/jquery-1.7.2.js" type="text/javascript"></script>
<script>
    $(function () {
        function logMsg(message) {
            $("#log").append("<li>" + message + "</li>");
        }

        logMsg("Attempting to connect to socket server");



        var server = null;
        try {
            server = new WebSocket("ws://localhost:8181/server");
            server.addEventListener("message", messageHandler, false);
            server.addEventListener("open", openHandler, false);
            server.addEventListener("close", closeHandler, false);
        }
        catch (e) {

        }

        function openHandler(e) {

            logMsg("Connection opened");
        }

        function closeHandler(e) {


            logMsg("Connection closed");
        }

        function messageHandler(e) {

            logMsg("Server says: " + e.data);
        }
    });
</script>
</html>

这是服务器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SocketServer.Models;

using Nugget;

namespace SocketServer
{
    class HelloServer : ISocketServer
    {
        private string _serverPath = string.Empty;
        private WebSocketServer _server = null;

        private string _input = string.Empty;

        public string Input
        {
            set { this._input = value; }
        }

        public void Initialize(string serverPath, string originServerAndPort)
        {
            this._server = new WebSocketServer(8181, originServerAndPort, "ws://localhost:8181");
            this._server.RegisterHandler<Socket>(serverPath);

            Nugget.Log.Level = LogLevel.None;
        }

        public void Start()
        {
            this._server.Start();
        }

        public void Send()
        {
            this._server.SendToAll(this._input);
        }
    }
}

这是启动服务器的控制台代码:

using System;
using Nugget;

namespace SocketServer
{
    class Server
    {
        static void Main(string[] args)
        {
            ISocketServer server = null;

            Console.WriteLine();
            Console.WriteLine("Welcome to the socket server.");
            Console.WriteLine();
            Console.WriteLine("Which server type would you like to create?");
            Console.WriteLine("   1) Hello World Server");
            Console.WriteLine("   2) Stocks Server");
            Console.WriteLine();

            string input = Console.ReadLine();

            if (input == "1")
            {
                server = new HelloServer();
                server.Initialize("/server", "http://localhost:2709");

                Console.WriteLine("");
                Console.WriteLine("The Hello World Server is now available under ws://localhost:8181/server.");
                Console.WriteLine("");
            }
            else if (input == "2")
            {
                server = new StockServer();
                server.Initialize("/stocks", "http://localhost:2709");

                Console.WriteLine("");
                Console.WriteLine("The Stocks Server is now available under ws://localhost:8181/stocks.");
                Console.WriteLine("");
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine(string.Format("Sorry, {0} is not a valid option.", input));
                Console.WriteLine("");
            }

            if (server != null)
            {
                server.Start();

                input = string.Empty;
                while (input != "exit")
                {
                    server.Input = input;
                    server.Send();
                    input = Console.ReadLine();
                }
            }

            Console.WriteLine("");
            Console.WriteLine("Closing socket server.");
            Console.WriteLine("");
        }
    }
}

我们遇到的问题是,客户端运行后它会给出此消息并且不连接到服务器:

尝试连接到套接字服务器
连接关闭

任何帮助将不胜感激。

4

1 回答 1

0

确保在您使用的浏览器中启用了 websocket。另一方面,您可能想在 client.html 中修改这一行

server = new WebSocket("ws://localhost:8181/server");

“/服务器”部分是必要的吗?

于 2012-08-21T12:17:19.037 回答