Just wondering if some can help me with the issue, below is my web socket Helloserver code and the client.htm code. After starting the Helloserver we run the client and notice that it does not connect to the server, will really appreciate if someone can help.
Here is the 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>
Here is the server code:
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);
}
}
}
Here is the console code to start the server:
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("");
}
}
}
The issue we are having is that after the client is run it gives this message and does not connect to the server:
Attempting to connect to socket server Connection closed
Any help will be appreciated.