本文介绍了Javascript客户端和C#服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给我一个简单的示例,说明如何连接这些2:

-在本地端口33333上工作的TCP侦听器服务器

-在浏览器上运行的Javascript客户端

我搜索了JS套接字,所有链接都转到了WebSockets或socket.io.如果服务器使用相同的框架创建,它们将非常有用.

你能帮忙吗? :D


客户端:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>

<script type="text/javascript">
// Create SocketIO instance, connect
var socket = new io.Socket(''localhost'',{
	port: 33333
});
socket.connect();

// Add a connect listener
socket.on(''connect'',function() {
	console.log(''Client has connected to the server!'');
});
// Add a connect listener
socket.on(''message'',function(data) {
	console.log(''Received a message from the server!'',data);
});
// Add a disconnect listener
socket.on(''disconnect'',function() {
	console.log(''The client has disconnected!'');
});

// Sends a message to the server via sockets
function sendMessageToServer(message) {
	socket.send(message);
}

</script>

<body>

</body>
</html>



JS中的此客户端代码甚至都没有连接到服务器.

提及:服务器与C#客户端一起使用.



Please give me an easy example on how can I connect these 2:

- TCP listener server working on localhost port 33333
and
- a Javascript client working on browser

I''ve searched JS sockets and all the links go to WebSockets or socket.io. They are very helpful if the server is created with the same framework.

Can you please help? :D

Edit:
Client side:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>

<script type="text/javascript">
// Create SocketIO instance, connect
var socket = new io.Socket(''localhost'',{
	port: 33333
});
socket.connect();

// Add a connect listener
socket.on(''connect'',function() {
	console.log(''Client has connected to the server!'');
});
// Add a connect listener
socket.on(''message'',function(data) {
	console.log(''Received a message from the server!'',data);
});
// Add a disconnect listener
socket.on(''disconnect'',function() {
	console.log(''The client has disconnected!'');
});

// Sends a message to the server via sockets
function sendMessageToServer(message) {
	socket.send(message);
}

</script>

<body>

</body>
</html>



This client code in JS doesn''t even connect to the server.

Mention: the server works with a C# client.

解决方案




这篇关于Javascript客户端和C#服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:00