1

I am trying to code client side of web socket connection on windows web form application with the help of SocketIO4Net library available in Visual Studio. Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SocketIOClient;
namespace WFA
{
    public partial class Form1 : Form
    {

        Client socket = new Client("ws://ec2-52-39-154-191.us-west-2.compute.amazonaws.com:8080");

        private void Form1_Load(object sender, EventArgs e)
        {
            socket.Connect();
        }
    }
}

I have instance on Amazon Web Services (AWS) with endpoint

ec2-52-39-154-191.us-west-2.compute.amazonaws.com

And I have app.js file on server that runs all the time

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

io.on('connection', function(socket){
  console.log("user connected");
});

http.listen(8080, function(){
  console.log('listening on *:8080');
});

This should simply display "listening on *:8080" and "user connected" message every time Form is loaded. I does display listening on *:8080 but doesn't display user connected when form is loaded. I don't understand where am I wrong. I thought may be its not working because its a public endpoint and not on the same network, due to settings on AWS.

So I tried this in web browser client side:

<!doctype html>
<html>
  <head>
  </head>
  <body>
    <div id = "chat">
        <ul id="messages"></ul>
    </div>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="/socket.io/socket.io.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
        var socket = io.connect('ws://ec2-52-39-154-191.us-west-2.compute.amazonaws.com:8080');
    </script>
  </body>
</html>

With this client code Every time someone visits the link http://ec2-52-39-154-191.us-west-2.compute.amazonaws.com:8080/ user connected is shown.

Why is it not showing when form is loaded compared to showing when someone visits the link. I would like the message to appear when someone loads the form.

M.Dagiya
  • 203
  • 3
  • 13

0 Answers0