Olivine-Labs / Alchemy-Websockets

An extremely efficient C# WebSocket server.
http://AlchemyWebsockets.net
Other
310 stars 106 forks source link

Unable to get server / client application working #62

Open vbguyny opened 11 years ago

vbguyny commented 11 years ago

I have a sample program which doesn't work. I created two multi-line textboxes and two buttons. The first click on button2 (which starts the server listening on port 81) and then click on button1 (which starts the client). The server errors out with an error of object reference is not set from the function Alchemy.WebSocketClient.SetupContext() and _context.Connection.client == null.

Could someone please explain why I am missing? Thanks, Mike.

CODE:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Alchemy;
using Alchemy.Classes;
using System.Net;

namespace AlchemyTest1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        WebSocketClient wsc;

        private void button1_Click(object sender, EventArgs e)
        {
            wsc.Connect();
            if (wsc.Connected == false)
            {
                return;
            }
            wsc.Send("Sup.");
            wsc.Disconnect();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            wsc = new WebSocketClient("ws://localhost:81/chat")
            {
                OnConnect = wsc_OnConnect,
                OnConnected = wsc_OnConnected,
                OnDisconnect = wsc_OnDisconnect,
                OnReceive = wsc_OnReceive,
                OnSend = wsc_OnSend
                //ConnectTimeout = new TimeSpan(0, 0, 10)
            };

            wss = new WebSocketServer(81, IPAddress.Any)
            {
                OnConnect = wss_OnConnect,
                OnConnected = wss_OnConnected,
                OnDisconnect = wss_OnDisconnect,
                OnReceive = wss_OnReceive,
                OnSend = wss_OnSend,
                TimeOut = new TimeSpan(0, 5, 0)
            };
        }

        private void wsc_OnConnect(UserContext context)
        {
            if (textBox1.InvokeRequired == true)
            {
                textBox1.Invoke(new MethodInvoker(delegate
                {
                    textBox1.AppendText("Connect to '" + context.ClientAddress.ToString() + "'\r\n");
                }));
            }
            else
            {
                textBox1.AppendText("Connect to '" + context.ClientAddress.ToString() + "'\r\n");
            }
        }

        private void wsc_OnConnected(UserContext context)
        {
            if (textBox1.InvokeRequired == true)
            {
                textBox1.Invoke(new MethodInvoker(delegate
                {
                    textBox1.AppendText("Connected to '" + context.ClientAddress.ToString() + "'\r\n");
                }));
            }
            else
            {
                textBox1.AppendText("Connected to '" + context.ClientAddress.ToString() + "'\r\n");
            }
        }

        private void wsc_OnDisconnect(UserContext context)
        {
            if (textBox1.InvokeRequired == true)
            {
                textBox1.Invoke(new MethodInvoker(delegate
                {
                    textBox1.AppendText("Disconnected to '" + context.ClientAddress.ToString() + "'\r\n");
                }));
            }
            else
            {
                textBox1.AppendText("Disconnected to '" + context.ClientAddress.ToString() + "'\r\n");
            }
        }

        private void wsc_OnReceive(UserContext context)
        {
            if (textBox1.InvokeRequired == true)
            {
                textBox1.Invoke(new MethodInvoker(delegate
                {
                    textBox1.AppendText("Recieved: " + context.DataFrame.ToString() + "\r\n");
                }));
            }
            else
            {
                textBox1.AppendText("Recieved: " + context.DataFrame.ToString() + "\r\n");
            }
        }

        private void wsc_OnSend(UserContext context)
        {
            if (textBox1.InvokeRequired == true)
            {
                textBox1.Invoke(new MethodInvoker(delegate
                {
                    textBox1.AppendText("Sending: " + context.DataFrame.ToString() + "\r\n");
                }));
            }
            else
            {
                textBox1.AppendText("Sending: " + context.DataFrame.ToString() + "\r\n");
            }
        }

        private void wss_OnConnect(UserContext context)
        {
            if (textBox2.InvokeRequired == true)
            {
                textBox2.Invoke(new MethodInvoker(delegate {
                    textBox2.AppendText("Connect to '" + context.ClientAddress.ToString() + "'\r\n");
                }));
            }
            else
            {
                textBox2.AppendText("Connect to '" + context.ClientAddress.ToString() + "'\r\n");
            }
        }

        private void wss_OnConnected(UserContext context)
        {
            if (textBox2.InvokeRequired == true)
            {
                textBox2.Invoke(new MethodInvoker(delegate
                {
                    textBox2.AppendText("Connected to '" + context.ClientAddress.ToString() + "'\r\n");
                }));
            }
            else
            {
                textBox2.AppendText("Connected to '" + context.ClientAddress.ToString() + "'\r\n");
            }
        }

        private void wss_OnDisconnect(UserContext context)
        {
            if (textBox2.InvokeRequired == true)
            {
                textBox2.Invoke(new MethodInvoker(delegate
                {
                    textBox2.AppendText("Disconnected to '" + context.ClientAddress.ToString() + "'\r\n");
                }));
            }
            else
            {
                textBox2.AppendText("Disconnected to '" + context.ClientAddress.ToString() + "'\r\n");
            }
        }

        private void wss_OnReceive(UserContext context)
        {
            if (textBox2.InvokeRequired == true)
            {
                textBox2.Invoke(new MethodInvoker(delegate
                {
                    textBox2.AppendText("Recieved: " + context.DataFrame.ToString() + "\r\n");
                }));
            }
            else
            {
                textBox2.AppendText("Recieved: " + context.DataFrame.ToString() + "\r\n");
            }
        }

        private void wss_OnSend(UserContext context)
        {
            if (textBox2.InvokeRequired == true)
            {
                textBox2.Invoke(new MethodInvoker(delegate
                {
                    textBox2.AppendText("Sending: " + context.DataFrame.ToString() + "\r\n");
                }));
            }
            else
            {
                textBox2.AppendText("Sending: " + context.DataFrame.ToString() + "\r\n");
            }
        }

        WebSocketServer wss;

        private void button2_Click(object sender, EventArgs e)
        {
            wss.Start();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            wsc.Disconnect();
            wss.Stop();
        }

    }
}
lincoln00 commented 10 years ago

I had this problem.

One of commits make this problem... (i don't know why)

Get this version e7a55eec49ae6c5fa2d507f81264932201e64e2f (old) and use this for compile a client dll.

Use this dll on your client project and it ll work...