TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Guest User
Tech Writer
82
7.3k
c sharp with socket programming
Sep 5 2019 3:50 AM
i have some code in mvc which are given error WebSocket connection to 'ws://localhost/api/Chatl?userid=12' failed: Error during WebSocket handshake: Unexpected response code: 404 how could resolve it??
this is chat.html file
<!DOCTYPE html
>
<
html
>
<
head
>
<
meta
charset
=
"utf-8"
/>
<
title
>
</
title
>
<!--<script src="Scripts/Socket.js"></script>-->
<
script
src
=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"
>
</
script
>
<
script
type
=
"text/javascript"
>
$(document).ready(function () {
var
userid
=
prompt
('Please enter a userid:');
var
uri
=
'ws://'
+ window.location.hostname + window.location.pathname.replace('chat.htm', 'api/Chat') + '?
userid
=' + userid;
websocket
=
new
WebSocket(uri);
websocket.onopen
=
function
() {
$('#messages').prepend('
<
div
>
Connected.
</
div
>
');
$('#chatform').submit(function (event) {
websocket.send($('#inputbox').val());
$('#inputbox').val('');
event.preventDefault();
});
};
websocket.onerror
=
function
(event) {
$('#messages').prepend('
<
div
>
ERROR
</
div
>
');
};
websocket.onmessage
=
function
(event) {
$('#btn').click(function () {
$('#messages').prepend('
<
div
>
' + event.data + '
</
div
>
');
});
};
});
</
script
>
</
head
>
<
body
>
<
form
id
=
"chatform"
action
=
""
>
<
input
id
=
"inputbox"
/>
<
input
type
=
"button"
id
=
"btn"
value
=
"BroadCast"
/>
</
form
>
<
div
id
=
"messages"
/>
</
body
>
</
html
>
this is webapi controller file
using
Microsoft.Web.WebSockets;
using
System;
using
System.Net;
using
System.Net.Http;
using
System.Web;
using
System.Web.Http;
namespace
socket_test
{
public
class
ChatController : ApiController
{
// [Route("api/WsHandler")]
[HttpGet]
public
HttpResponseMessage Get(
string
userid)
{
HttpContext.Current.AcceptWebSocketRequest(
new
ChatWebSocketHandler(userid));
return
Request.CreateResponse(HttpStatusCode.SwitchingProtocols);
}
class
ChatWebSocketHandler : WebSocketHandler
{
private
static
WebSocketCollection _chatClients =
new
WebSocketCollection();
private
string
_userid;
public
ChatWebSocketHandler(
string
userid)
{
_userid = userid;
}
public
override
void
OnOpen()
{
_chatClients.Add(
this
);
}
public
override
void
OnMessage(
string
message)
{
_chatClients.Broadcast(_userid +
": "
+ message);
}
}
}
}
Reply
Answers (
1
)
error code for Unable to read data from the transport conn..
Edit Row selected in gridview ,dropdown value get changed.