locked
How to push realtime data to clients in SignalR RRS feed

  • Question

  • User1049578906 posted

    I have recently heard about signalR. I want to display realtime data on web application. I have a console application in asp.net which receives realtime data from a device and sends on udp port. This udp data sent by console application should be displayed in web browsers in realtime. After exploring on internet i found a project which displays server system time on web page. But i want to modify it to receive my udp data from console application. After modification of ClockHub.cs I get error " System.Net.Sockets.SocketException: 'Only one usage of each socket address (protocol/network address/port) is normally permitted'

    "

    How should i modify it or please suggest other code which will solve my issue. Below is java scrypt code which displays PC time on web.

    Client.js

    var countryZone = "Bangladesh Standard Time";

    $(document).ready(function () {
        var hubProxy = $.connection.clockHub;

        hubProxy.client.setTime = function (time) {
            $('#clock').html(time);
        };

        $.connection.hub.start().done(function () {
            setInterval(function () {
                hubProxy.server.getTime(countryZone);
            }, 1000);
        });
    });
    function select_country() {
        countryZone = $("#lstCountry").val();
    }

    ClockHub.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Microsoft.AspNet.SignalR;

    namespace RealTimeClock.App_Code
    {
        public class ClockHub : Hub
        {
            public void getTime(string countryZone)
            {
               // TimeZone currentZone = TimeZone.CurrentTimeZone;
              //  DateTime currentDate = DateTime.Now;
               // DateTime currentUTC = currentZone.ToUniversalTime(currentDate);
               // TimeZoneInfo selectedTimeZone = TimeZoneInfo.FindSystemTimeZoneById(countryZone);
               // DateTime currentDateTime = TimeZoneInfo.ConvertTimeFromUtc(currentUTC, selectedTimeZone);

                 //My modified code to receive udp data from console application

    UdpClient receivingUdpClient = new UdpClient(11000);

    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

    Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);

    string returnData = Encoding.ASCII.GetString(receiveBytes);            

             Clients.Caller.setTime(returnData );
            }
        }
    }

    index.html

    
    

    <!DOCTYPE html>
    <html>
    <head>
        <title>SignalR Simple Chat</title>
        <style type="text/css">
            body {
                font-family: Verdana;
            }

            .container {
                width: 500px;
                margin: 0px auto;
            }

            #clock {
                margin-top: 100px;
                font-size: 70px;
                font-weight: bold;
            }

            #lstCountry {
                width: 100%;
                height: 40px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div style="font-weight:bold; line-height:50px;">Select Country:</div>
            <select id="lstCountry" onchange="select_country();">
                <option value="Bangladesh Standard Time">Bangladesh</option>
                <option value="Samoa Standard Time">Samoa</option>
                <option value="Hawaiian Standard Time">Hawaiian</option>
                <option value="Alaskan Standard Time">Alaskan</option>
                <option value="Pacific Standard Time (Mexico)">Pacific (Mexico)</option>
                <option value="Pacific Standard Time">Pacific</option>
                <option value="US Mountain Standard Time">US Mountain</option>
                <option value="Mountain Standard Time (Mexico)">Mountain (Mexico)</option>
                <option value="Mountain Standard Time">Mountain</option>
                <option value="Central America Standard Time">Central America</option>
                <option value="Central Standard Time">Central</option>
                <option value="Central Standard Time (Mexico)">Central (Mexico)</option>
                <option value="Canada Central Standard Time">Canada Central</option>
                <option value="SA Pacific Standard Time">SA Pacific</option>
                <option value="Eastern Standard Time">Eastern</option>
                <option value="US Eastern Standard Time">US Eastern</option>
                <option value="Venezuela Standard Time">Venezuela</option>
                <option value="Paraguay Standard Time">Paraguay</option>
                <option value="Atlantic Standard Time">Atlantic</option>
                <option value="Central Brazilian Standard Time">Central Brazilian</option>
                <option value="SA Western Standard Time">SA Western</option>
                <option value="Pacific SA Standard Time">Pacific SA</option>
                <option value="Newfoundland Standard Time">Newfoundland</option>
                <option value="E. South America Standard Time">E. South America</option>
                <option value="Argentina Standard Time">Argentina</option>
                <option value="SA Eastern Standard Time">SA Eastern</option>
                <option value="Greenland Standard Time">Greenland</option>
                <option value="Montevideo Standard Time">Montevideo</option>
                <option value="E. Europe Standard Time">E. Europe</option>
                <option value="Arabic Standard Time">Arabic</option>
                <option value="Russian Standard Time">Russian</option>
                <option value="Iran Standard Time">Iran</option>
                <option value="Pakistan Standard Time">Pakistan</option>
                <option value="India Standard Time">India</option>
                <option value="Sri Lanka Standard Time">Sri Lanka</option>
                <option value="Nepal Standard Time">Nepal</option>
                <option value="China Standard Time">China</option>
                <option value="Korea Standard Time">Korea</option>

            </select>
            <div id="clock">
            </div>
        </div>

    </body>
    <script src="/Scripts/jquery-1.6.4.min.js"></script>
    <script src="/Scripts/jquery.signalR-2.2.0.min.js"></script>
    <script src="/signalr/hubs"></script>
    <script src="/Scripts/Client.js"></script>
    </html>


    startup.cs

    using System;
    using System.Threading.Tasks;
    using Microsoft.Owin;
    using Owin;
    using Microsoft.AspNet.SignalR;

    [assembly: OwinStartup(typeof(RealTimeClock.Startup))]

    namespace RealTimeClock
    {
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                app.MapSignalR("/signalr", new HubConfiguration());
            }
        }
    }

    Thursday, February 25, 2021 11:07 AM

All replies

  • User-939850651 posted

    Hi chandradeor,

    I get error " System.Net.Sockets.SocketException: 'Only one usage of each socket address (protocol/network address/port) is normally permitted'

    Have you checked that the port is already occupied?

    Try something like this:

    1. Opened CMD and typed in : netstat -a
    2. Take a look in the Local Address column.
    3. Take a look at the port portion.
    4. Check whether the port of your project is already active (in use).
    5. Open task Manager and Stop that process
    6. Restart your project

    Hope this can help you.

    Best regards,

    Xudong Peng

    Friday, February 26, 2021 7:20 AM
  • User1049578906 posted

    I am not using the same port which is already used. This message is coming because socket  is called multiple times due to nature of web project

    Friday, February 26, 2021 9:41 AM
  • User-939850651 posted

    Hi chandradeor,

    I am not using the same port which is already used. This message is coming because socket  is called multiple times due to nature of web project

    It seems that you have found the cause of the problem. Have you solved it?

    If it has been resolved, can you post a solution to solve your problem, so that it will also be able to help other members of the community.

    Best regards,

    Xudong Peng

    Monday, March 1, 2021 7:10 AM