I am new to WebRTC and WebSockets and was following this tutorial to create a WebRTC demo project, but I am unable to create a WebSocket connection. I have followed the same steps as mentioned in the project.
His project is running on port 8080 and he mentioned ws://localhost:9090. My project is running on port 8081, but I copied his URL ws://localhost:9090 because I didn’t know the significance of 9090 and I received this error and my server is node.js. i changed local host to 8081 as well but then i am getting hand shake error.
WebSocket connection to ‘ws://localhost:9090/’ failed: Error in
connection establishment: net::ERR_CONNECTION_REFUSED.
Robert Harvey
176k47 gold badges333 silver badges497 bronze badges
asked Jun 15, 2017 at 19:58
5
Chrome doesn’t allow unsecure websocket (ws) connections to localhost (only wss, so you should setup a TLS certificate for your local web/websocket server).
However the same should work fine with Firefox.
answered Jul 17, 2017 at 18:50
IstvanIstvan
1,5011 gold badge13 silver badges19 bronze badges
1
You need to use ws://yourIp:9090/
, where yourIP is like 192.168.?.?
.
double-beep
4,85916 gold badges32 silver badges41 bronze badges
answered Feb 12, 2019 at 14:48
1
Usually WebRTC requires a secure connection (that is https).
The error you have got is due to TLS/SSL certificates occupied, may be they are not properly configured in your project.
Provide a valid TLS/SSL certificate and also configure it correctly in project, then it will work without the above error.
answered Oct 29, 2017 at 14:31
1
try to change the port to 8080
const ws = new WebSocket('ws://localhost:8080/chat')
answered Oct 11, 2019 at 15:13
fadi omarfadi omar
6975 silver badges14 bronze badges
2
I guess this is a generic websocket issue.
Change the url to a dynamic name using the built-in location.host
variable and change the protocol to secure websocket wss
if you have set-up the TLS:
const ws = new WebSocket("wss://" + location.host + "/")
answered Oct 11, 2020 at 14:44
go je jogo je jo
1714 silver badges8 bronze badges
Port 9090 is used by reactotron. Probably you are using it in your project and your app cannot connect with reactotron because it is closed. Just open reactotron and the error will disappear.
answered Nov 10, 2019 at 0:21
also you could easily change the mappings of IP addresses to host names,
on windows go to C:WindowsSystem32driversetchosts
and uncomment this line
127.0.0.1 localhost
save and restart.
monim
2,4382 gold badges7 silver badges17 bronze badges
answered May 20, 2021 at 8:16
WebSocket connection to ‘ws://localhost:8080/’ failed
Must ensure server file is running
I git this above problem
answered Aug 29, 2022 at 14:10
1
maybe you forgot to start websocket server, check it again, with configuration in my project, run:
php artisan websocket:init
answered Jan 31 at 2:52
I am new to WebRTC and WebSockets and was following this tutorial to create a WebRTC demo project, but I am unable to create a WebSocket connection. I have followed the same steps as mentioned in the project.
His project is running on port 8080 and he mentioned ws://localhost:9090. My project is running on port 8081, but I copied his URL ws://localhost:9090 because I didn’t know the significance of 9090 and I received this error and my server is node.js. i changed local host to 8081 as well but then i am getting hand shake error.
WebSocket connection to ‘ws://localhost:9090/’ failed: Error in
connection establishment: net::ERR_CONNECTION_REFUSED.
Robert Harvey
176k47 gold badges333 silver badges497 bronze badges
asked Jun 15, 2017 at 19:58
5
Chrome doesn’t allow unsecure websocket (ws) connections to localhost (only wss, so you should setup a TLS certificate for your local web/websocket server).
However the same should work fine with Firefox.
answered Jul 17, 2017 at 18:50
IstvanIstvan
1,5011 gold badge13 silver badges19 bronze badges
1
You need to use ws://yourIp:9090/
, where yourIP is like 192.168.?.?
.
double-beep
4,85916 gold badges32 silver badges41 bronze badges
answered Feb 12, 2019 at 14:48
1
Usually WebRTC requires a secure connection (that is https).
The error you have got is due to TLS/SSL certificates occupied, may be they are not properly configured in your project.
Provide a valid TLS/SSL certificate and also configure it correctly in project, then it will work without the above error.
answered Oct 29, 2017 at 14:31
1
try to change the port to 8080
const ws = new WebSocket('ws://localhost:8080/chat')
answered Oct 11, 2019 at 15:13
fadi omarfadi omar
6975 silver badges14 bronze badges
2
I guess this is a generic websocket issue.
Change the url to a dynamic name using the built-in location.host
variable and change the protocol to secure websocket wss
if you have set-up the TLS:
const ws = new WebSocket("wss://" + location.host + "/")
answered Oct 11, 2020 at 14:44
go je jogo je jo
1714 silver badges8 bronze badges
Port 9090 is used by reactotron. Probably you are using it in your project and your app cannot connect with reactotron because it is closed. Just open reactotron and the error will disappear.
answered Nov 10, 2019 at 0:21
also you could easily change the mappings of IP addresses to host names,
on windows go to C:WindowsSystem32driversetchosts
and uncomment this line
127.0.0.1 localhost
save and restart.
monim
2,4382 gold badges7 silver badges17 bronze badges
answered May 20, 2021 at 8:16
WebSocket connection to ‘ws://localhost:8080/’ failed
Must ensure server file is running
I git this above problem
answered Aug 29, 2022 at 14:10
1
maybe you forgot to start websocket server, check it again, with configuration in my project, run:
php artisan websocket:init
answered Jan 31 at 2:52
Hello everyone!
I’m having some problems using WebSocket WS in my application.
After many tests on my localhost, where everything works perfectly, I placed the application in a virtual machine in google cloud and the code simply stopped working, presenting the following error:
WebSocket connection to 'ws://localhost:8000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
This is the code that has stopped working (in bold):
import Vue from 'vue'
import Vuex from 'vuex'
import shortid from 'shortid'
import { WebSocketBridge } from 'django-channels'
import { randomUsername } from '@/utils'
import router from '@/router'
Vue.use(Vuex)
const bus = new Vue()
const socket = new WebSocketBridge()
const state = {
lobby: {},
socket: socket,
socketConnected: false,
bus: bus,
emulator: {
state: 'chilling',
},
}
const getters = {
bus: state => () => state.bus,
store: state => () => state.store,
userIsHost: (state, getters) => match => {
return state.user.identifier === match.host.identifier
},
}
export const SHOW_CONTROLS_DEFAULT = false
export const LOBBY_LIST_REQUEST = 'lobby.list_request'
export const LOBBY_LIST_RESPONSE = 'lobby.list_response'
export const LOBBY_GET_BY_ID = 'lobby.get_by_id'
export const MATCH_CREATED = 'match.created'
export const MATCH_UPDATED = 'match.updated'
export const MATCH_DELETED = 'match.deleted'
export const MATCH_HOST = 'match.host'
export const MATCH_JOIN = 'match.join'
export const MATCH_LEAVE = 'match.leave'
export const MATCH_ADD_PLAYER = 'match.add_player'
export const MATCH_PAIRING_ANSWER = 'match.rtc_answer'
export const MATCH_PAIRING_OFFER = 'match.rtc_offer'
export const MATCH_PAIRING_CANDIDATE = 'match.rtc_candidate'
export const MATCH_KICK_PLAYER = 'match.kick_player'
export const MATCH_PAIR_CONTROLLER = 'match.pair_controller'
export const MATCH_CLOSE = 'match.close'
export const MATCH_STATS_START = 'match.stats_start'
export const MATCH_CONTROLS_SHOW = 'match.controls_show'
export const MATCH_CONTROLS_HIDE = 'match.controls_hide'
export const MATCH_FULLSCREEN = 'emulator.fullscreen'
export const CHAT_MESSAGE = 'chat.relay_message'
export const CHAT_FOCUS_ENTER = 'chat.focus_enter'
export const CHAT_FOCUS_EXIT = 'chat.focus_exit'
export const USER_DISCONNECT = 'user.disconnect'
export const USER_HEARTBEAT = 'user.heartbeat'
export const ROM_STORE = 'rom_store'
export const EMULATOR_STATE = 'emulator.state'
export const EMULATOR_PLAY = 'emulator.play'
const objectWithoutKey = (object, key) => {
const {[key]: deletedKey, ...otherKeys} = object
return otherKeys
}
const actions = {
[MATCH_HOST] ({commit, state}, match) {
const matchChannelName = Object.keys(match)
const matchWithoutRom = objectWithoutKey(match[matchChannelName], 'rom')
console.log(matchWithoutRom)
commit(MATCH_HOST, match)
commit(EMULATOR_STATE, {state: 'loading'})
state.socket.stream(MATCH_HOST).send(matchWithoutRom)
},
[MATCH_JOIN] ({commit, state}, match) {
const join = {
identifier: state.user.identifier,
match: match.channel_name,
username: state.user.username,
}
state.socket.stream(MATCH_JOIN).send(join)
},
[MATCH_ADD_PLAYER] ({commit, state}, match) {
console.log('MATCH_ADD_PLAYER')
console.log(match)
const join = {
identifier: shortid.generate(),
match: match.channel_name,
username: randomUsername(),
}
state.socket.stream(MATCH_ADD_PLAYER).send(join)
},
[MATCH_PAIR_CONTROLLER] ({commit, state}, payload) {
const match = payload.match
const userIdentifier = payload.userIdentifier
console.log('MATCH_PAIR_CONTROLLER')
console.log(match)
const join = {
identifier: userIdentifier,
match: match.channel_name,
username: 'egal',
}
state.socket.stream(MATCH_PAIR_CONTROLLER).send(join)
},
[MATCH_KICK_PLAYER] ({commit, state}, payload) {
state.socket.stream(MATCH_KICK_PLAYER).send(payload)
},
[MATCH_CLOSE] ({commit, state}, payload) {
state.socket.stream(MATCH_CLOSE).send(payload)
},
}
const mutations = {
[ROM_STORE] (state, payload) {
state.rom = payload.rom
},
[LOBBY_GET_BY_ID] (state, match) {
console.log(match)
},
[LOBBY_LIST_RESPONSE] (state, payload) {
state.lobby = payload
},
[MATCH_CREATED] (state, payload) {
state.lobby = {
...state.lobby,
...{[payload.channel_name]: payload},
}
},
[MATCH_UPDATED] (state, payload) {
state.lobby = {
...state.lobby,
[payload.channel_name]: payload,
}
},
[MATCH_DELETED] (state, payload) {
let {[payload.channel_name]: foo, ...newLobby} = state.lobby
state.lobby = newLobby
},
[MATCH_HOST] (state, payload) {
state.lobby = {
...state.lobby,
...payload,
}
const channelName = Object.keys(payload)[0]
router.push({
name: 'match',
params: {
channel_name: channelName,
host: true,
},
})
},
[MATCH_JOIN] (state, join) {
const kJoin = {
[join.identifier]: join,
}
const match = state.lobby[join.match]
Vue.set(match, 'player_count', match.player_count + 1)
match.takers = {
...match.takers,
...kJoin,
}
if (join.identifier === state.user.identifier) {
state.user = {
...state.user,
port: join.port,
}
}
router.push({
name: 'match',
params: {
channel_name: match.channel_name,
host: false,
},
})
},
[MATCH_ADD_PLAYER] (state, join) {
const kJoin = {
[join.identifier]: join,
}
const match = state.lobby[join.match]
Vue.set(match, 'player_count', match.player_count + 1)
match.takers = {
...match.takers,
...kJoin,
}
},
[MATCH_PAIR_CONTROLLER] (state, join) {
const kJoin = {
[join.identifier]: join,
}
const match = state.lobby[join.match]
match.takers = {
...match.takers,
...kJoin,
}
},
[MATCH_PAIRING_OFFER] (state, foo) {
// console.log(foo)
},
[MATCH_PAIRING_ANSWER] (state, foo) {
// console.log(foo)
},
[MATCH_PAIRING_CANDIDATE] (state, foo) {
// console.log(foo)
},
[MATCH_KICK_PLAYER] (state, payload) {
const match = state.lobby[payload.match]
Vue.set(match, 'player_count', match.player_count - 1)
const {[payload.taker]: foo, ...newTakers} = match.takers
match.takers = newTakers
},
[MATCH_LEAVE] (state, payload) {
const match = state.lobby[payload.match]
Vue.set(match, 'player_count', match.player_count - 1)
const {[payload.taker]: foo, ...newTakers} = match.takers
match.takers = newTakers
},
[CHAT_MESSAGE] (state, message) {
console.log('Got chat message:', message)
const match = state.lobby[message.match]
if (!match.hasOwnProperty('chat')) {
match['chat'] = []
}
match['chat'].push(message)
},
[USER_DISCONNECT] (state, payload) {
console.log('USER_DISCONNECT')
if (Object.keys(payload).length === 0 && payload.constructor === Object) {
return
}
const match = state.lobby[payload.match]
Vue.set(match, 'player_count', match.player_count - 1)
if (match.host.identifier === payload.user) {
let key = payload.match
let {[key]: dontMatter, ...newLobby} = state.lobby
state.lobby = newLobby
} else {
console.log('TAKER!')
const {[payload.user]: foo, ...newTakers} = match.takers
match.takers = newTakers
}
},
[EMULATOR_STATE] (state, payload) {
console.log('Setting new emulator state:', payload.state)
Vue.set(state.emulator, 'state', payload.state)
},
[MATCH_STATS_START] (state, payload) {
},
}
**function createWebSocketPlugin (socket, bus) {
return store => {
var url = 'ws://localhost:8000'
if (process.env.NODE_ENV === 'testing') {
url = process.env.SMES_BACKEND_URL.replace('http://', 'ws://')**
}
socket.connect(url)
socket.listen()
const channels = ['chat', 'lobby', 'match', 'user']
channels.forEach(function (channel) {
socket.demultiplex(channel, (message, stream) => {
const mutationType = stream + '.' + message.action
store.commit(mutationType, message.payload)
const payloadCopy = JSON.parse(JSON.stringify(message.payload))
bus.$emit(mutationType, payloadCopy)
})
})
socket.socket.addEventListener('open', function () {
console.log('Connected to websocket backend:', url)
store.state.socketConnected = true
socket.stream(LOBBY_LIST_REQUEST).send({})
})
socket.socket.addEventListener('close', function () {
console.log('Disconnected from backend:', url)
store.state.socketConnected = false
})
}
}
const plugin = createWebSocketPlugin(socket, bus)
export default new Vuex.Store({
modules: {},
state,
getters,
actions,
mutations,
plugins: [plugin],
})
Any help?
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
<?php namespace RatchetRFC6455Handshake; use PsrHttpMessageRequestInterface; use GuzzleHttpPsr7Response; /** * The latest version of the WebSocket protocol * @todo Unicode: return mb_convert_encoding(pack("N",$u), mb_internal_encoding(), 'UCS-4BE'); */ class ServerNegotiator implements NegotiatorInterface { /** * @var RatchetRFC6455HandshakeRequestVerifier */ private $verifier; private $_supportedSubProtocols = []; private $_strictSubProtocols = false; public function __construct(RequestVerifier $requestVerifier) { $this->verifier = $requestVerifier; } /** * {@inheritdoc} */ public function isProtocol(RequestInterface $request) { return $this->verifier->verifyVersion($request->getHeader('Sec-WebSocket-Version')); } /** * {@inheritdoc} */ public function getVersionNumber() { return RequestVerifier::VERSION; } /** * {@inheritdoc} */ public function handshake(RequestInterface $request) { if (true !== $this->verifier->verifyMethod($request->getMethod())) { return new Response(405, ['Allow' => 'GET']); } if (true !== $this->verifier->verifyHTTPVersion($request->getProtocolVersion())) { return new Response(505); } if (true !== $this->verifier->verifyRequestURI($request->getUri()->getPath())) { return new Response(400); } if (true !== $this->verifier->verifyHost($request->getHeader('Host'))) { return new Response(400); } $upgradeSuggestion = [ 'Connection' => 'Upgrade', 'Upgrade' => 'websocket', 'Sec-WebSocket-Version' => $this->getVersionNumber() ]; if (count($this->_supportedSubProtocols) > 0) { $upgradeSuggestion['Sec-WebSocket-Protocol'] = implode(', ', array_keys($this->_supportedSubProtocols)); } if (true !== $this->verifier->verifyUpgradeRequest($request->getHeader('Upgrade'))) { return new Response(426, $upgradeSuggestion, null, '1.1', 'Upgrade header MUST be provided'); } if (true !== $this->verifier->verifyConnection($request->getHeader('Connection'))) { return new Response(400, [], null, '1.1', 'Connection Upgrade MUST be requested'); } if (true !== $this->verifier->verifyKey($request->getHeader('Sec-WebSocket-Key'))) { return new Response(400, [], null, '1.1', 'Invalid Sec-WebSocket-Key'); } if (true !== $this->verifier->verifyVersion($request->getHeader('Sec-WebSocket-Version'))) { return new Response(426, $upgradeSuggestion); } $headers = []; $subProtocols = $request->getHeader('Sec-WebSocket-Protocol'); if (count($subProtocols) > 0 || (count($this->_supportedSubProtocols) > 0 && $this->_strictSubProtocols)) { $subProtocols = array_map('trim', explode(',', implode(',', $subProtocols))); $match = array_reduce($subProtocols, function($accumulator, $protocol) { return $accumulator ?: (isset($this->_supportedSubProtocols[$protocol]) ? $protocol : null); }, null); if ($this->_strictSubProtocols && null === $match) { return new Response(426, $upgradeSuggestion, null, '1.1', 'No Sec-WebSocket-Protocols requested supported'); } if (null !== $match) { $headers['Sec-WebSocket-Protocol'] = $match; } } return new Response(101, array_merge($headers, [ 'Upgrade' => 'websocket' , 'Connection' => 'Upgrade' , 'Sec-WebSocket-Accept' => $this->sign((string)$request->getHeader('Sec-WebSocket-Key')[0]) , 'X-Powered-By' => 'Ratchet' ])); } /** * Used when doing the handshake to encode the key, verifying client/server are speaking the same language * @param string $key * @return string * @internal */ public function sign($key) { return base64_encode(sha1($key . static::GUID, true)); } /** * @param array $protocols */ function setSupportedSubProtocols(array $protocols) { $this->_supportedSubProtocols = array_flip($protocols); } /** * If enabled and support for a subprotocol has been added handshake * will not upgrade if a match between request and supported subprotocols * @param boolean $enable * @todo Consider extending this interface and moving this there. * The spec does says the server can fail for this reason, but * it is not a requirement. This is an implementation detail. */ function setStrictSubProtocolCheck($enable) { $this->_strictSubProtocols = (boolean)$enable; } } |
Moderators: Lapo, Bax
-
pogsdbest
- Posts: 17
- Joined: 08 Feb 2019, 10:09
[SOLVED] WebSocket Connection Error
Hi guys im having an Error Connecting using My webgl build in unity
Code: Select all
port = 8443;
sfs = new SmartFox(UseWebSocket.WSS_BIN);
ConfigData cfg = new ConfigData ();
cfg.Host = serverHost;
cfg.Port = port;
cfg.Zone = defaultZone;
sfs.Connect ( cfg );
WebSocket connection to ‘wss://myip/websocket’ failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID
on my Machine it connects, but when i send the link of my webgl build to others they get this error, is there anyone here encounter this before.
im using cloudflare.
Last edited by pogsdbest on 26 Feb 2019, 09:06, edited 1 time in total.
-
Lapo
- Site Admin
- Posts: 22613
- Joined: 21 Mar 2005, 09:50
- Location: Italy
Re: WebSocket Connection Error
Postby Lapo » 21 Feb 2019, 08:02
Hi,
you’re using port 8443, which is dedicated to WSS (secure websocket).
In order to use WSS you must have a domain and valid SSL certificate, which is not possible for local testing.
I recommend simply using port 8080 instead, which is the standard for non encrypted websockets.
Hope it helps
-
pogsdbest
- Posts: 17
- Joined: 08 Feb 2019, 10:09
Re: WebSocket Connection Error
Postby pogsdbest » 22 Feb 2019, 06:17
thanks lapo, but we already have a domain and a valid certificate from cloudflare. and on our testing it is already working but only on my machine. when someone try to visit the webGL build that i uploaded to our domain. it gives me the said error
-
Bax
- Site Admin
- Posts: 4579
- Joined: 29 Mar 2005, 09:50
- Location: Italy
- Contact:
Re: WebSocket Connection Error
Postby Bax » 22 Feb 2019, 09:26
We noticed that the connection url uses the unusual «blob» system. Is there a reason for that? Could it be interfering with the certificate?
I suggest to test with a plain url.
Paolo Bax
The SmartFoxServer Team
-
pogsdbest
- Posts: 17
- Joined: 08 Feb 2019, 10:09
Re: WebSocket Connection Error
Postby pogsdbest » 26 Feb 2019, 07:41
Hi Bax i tried the plain Url but still get the error
Last edited by pogsdbest on 26 Feb 2019, 08:00, edited 1 time in total.
-
Lapo
- Site Admin
- Posts: 22613
- Joined: 21 Mar 2005, 09:50
- Location: Italy
Re: WebSocket Connection Error
Postby Lapo » 26 Feb 2019, 07:57
If you’re running a valid SSL certificate you must connect to your server via it’s domain name, instead of it’s IP address.
This is because the SSL certificate only works with domains, hence the SSL validation error.
Hope it helps
-
moonlife
- Posts: 44
- Joined: 26 Sep 2016, 08:22
Re: [SOLVED] WebSocket Connection Error
Postby moonlife » 29 Jan 2021, 14:07
Hi Lapo, I have same problem,
I cannot login to smartfoxserver on websocket with ssl.
I’ve also tried with sample project called «AdvancedConnector».
I’ve valid ssl and websocket property is open on my admin tool.
What should I do?
I can send you message the URL
-
Lapo
- Site Admin
- Posts: 22613
- Joined: 21 Mar 2005, 09:50
- Location: Italy
Re: [SOLVED] WebSocket Connection Error
Postby Lapo » 29 Jan 2021, 14:47
HI,
sure you can send the URL in a private message.
However we also need to know what client API you’re using (and what version) and what errors are you getting both on client and server sides when the connection fails.
Thanks
-
moonlife
- Posts: 44
- Joined: 26 Sep 2016, 08:22
Re: [SOLVED] WebSocket Connection Error
Postby moonlife » 29 Jan 2021, 15:46
Hi,
I’ve sent a message to you. I use unity3d 2019.4.12f
it gives error like this
I’ve tried both with ip address and domain name
WebSocket connection to ‘wss://ipadres:8443/websocket’ failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID
thanks
-
Lapo
- Site Admin
- Posts: 22613
- Joined: 21 Mar 2005, 09:50
- Location: Italy
Re: [SOLVED] WebSocket Connection Error
Postby Lapo » 29 Jan 2021, 16:31
Hi,
you can’t use the IP address with SSL, because the certificate is tied to your domain name, not the IP address.
You must use the domain name when connecting from client side.
Cheers
-
moonlife
- Posts: 44
- Joined: 26 Sep 2016, 08:22
Re: [SOLVED] WebSocket Connection Error
Postby moonlife » 29 Jan 2021, 16:43
Hi,
I’ve also tried with domain name, but it given same error. You can try with domain that I’ve sent you in private message
-
Lapo
- Site Admin
- Posts: 22613
- Joined: 21 Mar 2005, 09:50
- Location: Italy
Re: [SOLVED] WebSocket Connection Error
Postby Lapo » 30 Jan 2021, 16:01
Hi,
I just checked your domain in the browser on port 8443, which is the default for SFS2X and the page shows an SSL error, saying that there is no valid SSL certificate deployed.
That is the problem for sure
You have to deploy the a valid SSL certificate in SFS2, as per the docs here:
http://docs2x.smartfoxserver.com/Gettin … yptography
Cheers
Return to “SFS2X HTML5 / JavaScript API”
Who is online
Users browsing this forum: No registered users and 1 guest