I could connect websocket to the server on my local machine. But when i uploaded the file to a remote ubuntu server, it doesn't work any more.
Server side code(server.php):
$master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($master, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($master, "127.0.0.1", 80);
socket_listen($master, 20);
client side code:
<script type="text/javascript">
var host = "ws://127.0.0.1:80/server.php";
socket = new WebSocket(host);
</script>
I open the client page from chrome canary version 24, server side didn't get any accepted socket.
I changed the host to "ws://xx.xx.xx.xx:80/server.php" to the real IP address of the server, doesn't work. Also changed the server side socket_bind($master, "127.0.0.1", 80) to real ip, no luck either.
Any one can help me?
Thanks,
Jasmine
My friend, when you write your server. You need to keep it running as a service or just keep it running.
In the case of a php server, you need to run that file from the PHP Cli (php yourfile.php), not from the browser (You can, setting the timeout to 0, but if you close the browser, your server stops working).
I really don't recommend a socket server over PHP. Read more about socket servers with C++ (QT, Berkeley), or Java Socket Servers.
When we are talking about HTML5 Websockets Server, it's different from programming a 'normal' socket server (There are a lot of libraries for PHP that can help you programming a HTML5 Websocket server)
https://github.com/Flynsarmy/PHPWebSocket-Chat
In that link, you can find an example of a chat. There is a file called class.PHPWebSocket.php, include it in your server.php and use that library for the creation of the HTML5 Websocket Server. See the examples, read how it works and how to use the class.PHPWebSocket.php (Take a look at the server.php file inside the examples).
Run the server when you finish it.
Then do:
// ...
var host = "ws://IP.OF.THE.SERVER:port";
socket = new WebSocket(host);
// ...
Read more about socket servers, HTML5 Websockets, and take a look at that class.PHPWebSocket.php utility.
Good Luck My Friend.
Related
I'm a beginner in Rust and WebSockets and I'm trying to deploy on Heroku a little chat backend I wrote (everything works on localhost). The build went well and I can see the app is running, and I'm now trying to connect to the WebSocket from a local HTML/Javascript frontend, but it is not working.
Here is my code creating the WebSocket on my rust server on Heroku (using the tungstenite WebSocket crate):
async fn main() -> Result<(), IoError> {
let port = env::var("PORT").unwrap_or_else(|_| "8080".to_string());
let addr = format!("0.0.0.0:{}", port);
// Create the event loop and TCP listener we'll accept connections on.
let try_socket = TcpListener::bind(&addr).await;
let listener = try_socket.expect("Failed to bind");
println!("Listening on: {}", addr);
and here is the code in my Javascript file that tries to connect to that WebSocket:
var ws = new WebSocket("wss://https://myappname.herokuapp.com/");
My web client gets the following error in the console:
WebSocket connection to 'wss://https//rocky-wave-51234.herokuapp.com/' failed
I searched to find the answer to my issue but unfortunately didn't find a fix so far. I've found hints that I might have to create an HTTP server first in my backend and then upgrade it to a WebSocket, but I can't find a resource on how to do that and don't even know if this is in fact the answer to my problem. Help would be greatly appreciated, thanks!
I think your mistake is the URL you use:
"wss://https://myappname.herokuapp.com/"
A URL usually starts with <protocol>://. The relevant protocols here are:
http - unencrypted hypertext
https - encrypted hypertext
ws - unencrypted websocket
wss - encrypted websocket
So if your URL is an encrypted websocket, it should start only with wss://, a connection cannot have multiple protocols at once:
"wss://myappname.herokuapp.com/"
I installed a nodeJS server behind a reverse proxy with an alias.
Basically i'd like to reach my nodeJS server on : http://myapp.domain.intra/chat
but it seems that when, on the client, i do : var socket = io("http://myapp.domain.intra/chat",...)
the '/chat' is truncated and the connection tries to connect on the domain only : 'myapp.domain.intra', which does not work.
I do understand that basically the /chat is interpreted as a scope but how can I force socket IO take the /chat in account as the connection url?
Thanks
Ok, I found it. It's pretty easy.
I needed to set the path option like so :
var socket = io.connect("http://myapp.domain.intra",{path:'/chat/socket.io'});
1-Xampp
2-laravel 5.1
3-run localhost:8000
class in function
$redis = LRedis::connection();
$redis->publish('message', "fgfgff");
error
ConnectionException in AbstractConnection.php line 155:
No connection could be made because the target machine actively refused it. [tcp://127.0.0.1:6379]
If you are trying to create a connection between 2 host Redis is not an answer for you.
check out this question about what is Redis:
What is Redis and what do I use it for?
The problem with the Connection is probably because of the URL sending to the client side socket constructor.
change : var socket = io('http://localhost');
to : var socket = io();
Here is a link on step by step guide to use socket.io:
search "use socket.io with laravel" in Google.
To use connection between 2 host with Laravel you should use a package as it is not in Laravel by default.
this package enables push notification between server and devices like android or ios phones. here is the link to the package in GitHub:
davibennun/laravel-push-notification
Hope to be useful.
good luck.
I have the app running and it doesn't find socket.io and gives me 404.
this is the client:
<script src="http://game-test-1.herokuapp.com/socket.io/socket.io.js"></script>
<script>var socket = io.connect('http://game-test-1.herokuapp.com');</script>
and this is the server:
var express = require('express'),
routes = require('./routes'),
user = require('./routes/user'),
http = require('http'),
path = require('path'),
express = require("express"),
app = express(),
server = http.createServer(app),
io = require('socket.io').listen(server);
server.listen(80);
I tried changing the ports and doing http://game-test-1.herokuapp.com:81/... but that gave me an error
How do I link socket.io correctly?
Heroku requires that your application listens on a port of Heroku's choosing. Heroku will tell your application this port when it launches your application, via the PORT environment variable. If you listen on any other port, Heroku will ignore it and will terminate your application after 60 seconds.
Your users' browsers will still use ports 80 for HTTP traffic and 443 for HTTPS traffic. Heroku's routing layer, which is in-between the user's browser and your application, will send the HTTP traffic through to your application at the port Heroku designated for your application.
Heroku Dev Center has a guide on using socket.io on Heroku. In particular, you'll need this bit of config:
// assuming io is the Socket.IO server object
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
This is because Heroku does not support Websockets at this time (to the best of my knowledge) In order to use socket.io you need to force it to fallback to an xhr long-polling approach.
First of all, sorry for my bad English : )
My Java application (multiplayer game server) uses this package to communicate with a web application in client's browser using websockets: https://github.com/TooTallNate/Java-WebSocket
I've encountered a problem running my application: only I can connect to the websocket server, clients on other hosts can't do so. In browser I estabilish connection as usual, address here is certainly correct:
new WebSocket("ws://"+serverIp+":8787");
When I connect from my own host to the websocket server running on the same host, it runs perfectly. When other hosts try to connect to me, connection in not being estabilished: in browser WebSocket objects's .readyState is 0 (whilst it should be 1), and even server does not recieve any handshakes (no output from onClientOpen in server console, I even tried to get any output from certain WebSocketServer class' methods).
Other hosts are still recieving, for example, static contents of web application from webserver on 80 port on the same host. Problem is not the closed 8787 port: I checked it, it's open.
What may be the reason that other host can't connect to my websocket server?
WebSockets uses a cross-origin permission system. You might need to tell you WebSocket server to accept connections from more than just your local host. The verification of Origin happens during the WebSocket handshake which likely happens prior to onclientOpen.